Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7812485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:31:19+00:00 2026-06-02T04:31:19+00:00

For some reason my ASP.NET MVC3 webpage keeps throwing me NullReferenceExceptions. The weird things

  • 0

For some reason my ASP.NET MVC3 webpage keeps throwing me NullReferenceExceptions. The weird things is that it is impossible for me to identify the source of the problem. No matter what i write in my view the first piece of code (in this case my foreach) is said to cause this error.

my view looks like this:

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<SkyLearn.Models.StartpageDataViewModel>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Skylearn - Startside
</asp:Content>

<asp:Content ID="Content3" ContentPlaceHolderID="SideContent" runat="server">
        <%foreach (var category in Model.CurrentUsersCategories)
          {%>
             <div class="homecategory"><div class="homecategoryicon"></div><%: Html.DisplayFor(Title => category.Title)%></div>
        <%}%>       
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <%: ViewBag.Message %>
</asp:Content>

And this is the viewmodel:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Web.Mvc;
using System.Web.Security;
using SkyLearn.Areas.Categories.Models;
using SkyLearn.Areas.Categories.Controllers;

namespace SkyLearn.Models
{
    public class StartpageDataViewModel
    {
        public List<Category> CurrentUsersCategories { get; set; }

        public StartpageDataViewModel()
        { 
           CurrentUsersCategories = new List<Category>();
        }
    }
}

and finally the methods i use from 2 different controllers:

this is the one that puts data in the viewmodel object:

public ActionResult Index()
        {
            if (Request.IsAuthenticated)
            {
                StartpageDataViewModel model = new StartpageDataViewModel();

                MembershipUser currentuser = Membership.GetUser();
                List<Category> categories = new List<Category>();

                categories = accountcontroller.getCategoriesByRoles((Guid)currentuser.ProviderUserKey);

                foreach (Category category in categories)
                {
                    model.CurrentUsersCategories.Add(category);
                } 

                ViewBag.Message = "Velkommen til Skylearn Video Tutorials " + currentuser.UserName + "!";

                return View(model);
            }
            else 
            {
                ViewBag.Message = "Log venligst ind for at benytte Skylearn Video Tutorials";

                return View();
            }
        }

And this is the one that gets some of the data:

public List<Category> getCategoriesByRoles(Guid userid)
        {
            List<Category> categoriesbyrole = new List<Category>();

            MembershipUser user = Membership.GetUser(userid);

            string[] roles = Roles.GetRolesForUser(user.UserName);

            List<Category> categories = categorycontroller.getCategories();

            foreach (string role in roles)
            {
                foreach (Category category in categories)
                {
                    if (role == category.Title)
                    {
                        categoriesbyrole.Add(category);
                    }
                }
            }

            return categoriesbyrole;
        }

no matter what i do i get the current error on the foreach in my view:

System.NullReferenceException was unhandled by user code
Message=Object reference not set to an instance of an object.
Source=App_Web_nvi5brwn StackTrace:
at ASP.views_home_index_aspx.__RenderContent3(HtmlTextWriter __w, Control parameterContainer) in c:\Users\AronChan\Desktop\Dropbox\SkyLearn\Skylearn Website (Aron
sikkerhedskopi)\SkyLearn\Views\Home\Index.aspx:line 8
at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)
at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)
at System.Web.UI.Control.Render(HtmlTextWriter writer)
at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
at System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
at ASP.views_shared_site_master._Render_control1(HtmlTextWriter __w,
Control parameterContainer) in
c:\Users\AronChan\Desktop\Dropbox\SkyLearn\Skylearn Website (Aron
sikkerhedskopi)\SkyLearn\Views\Shared\Site.Master:line 34
at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)
at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)
at System.Web.UI.Control.Render(HtmlTextWriter writer)
at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
at System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)
at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)
at System.Web.Mvc.ViewPage.Render(HtmlTextWriter writer)
at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
at System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
InnerException:

I have tried everything i could think of. Please help me xD

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T04:31:23+00:00Added an answer on June 2, 2026 at 4:31 am

    Your problem is probably coming from your else in Index. You are not passing a model in this case. So, Model will be null in this case. I would try sending a new model that is empty to test and verify.

    else 
            {
                ViewBag.Message = "Log venligst ind for at benytte Skylearn Video Tutorials";
    
                return View();
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got an ASP.NET app that requires Windows Integrated Security. For some reason, I
I've started work on an ASP.Net project and for some reason Visual Studio keeps
I have an ASP.NET AJAX page that for some reason on the LIVE version
For some reason, any CalendarExtenders on an ASP.NET site that is being worked on
Hey folks, I've got an ASP.NET web site project that for some reason is
I'm using ASP.NET MVC 3 and DataAnnotations validations and for some reason the javascript
I have an asp.net mvc3 project, it has some reports in aspx web pages.
I have an ASP.NET MVC App, which use EF code First, for some reason
I have a wierd problem with threading in an ASP.NET application. For some reason,
For some reason, ASP.NET code on my server is now returning a format of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.