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