MVC Error: Object reference not set to an instance of an object.

Oct 20, 2010 at 4:56 PM

I'm close to giving up on this mvc app for today!!

I'm following the Mvc Music Store Tutorial and I'm stuck on page 54.

this is the error I'm getting: System.NullReferenceException: Object reference not set to an instance of an object.

The error occurs in the third paragraph block (dropdownlist) in the following code(Album.ascx):

 

<%@ Import Namespace ="MvcMovies1" %>
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MvcMovies1.Models.Album>" %>

<p>
� � <%: Html.LabelFor(model => model.Title) %>
� � <%: Html.TextAreaFor(model => model.Title) %>
� � <%: Html.ValidationMessageFor(model => model.Title) %>
</p>

<p>
� � <%: Html.LabelFor(model => model.Price) %>
� � <%: Html.TextAreaFor(model => model.Price) %>
� � <%: Html.ValidationMessageFor(model => model.Price) %>
</p>

<p>
� � <%: Html.LabelFor(model => model.AlbumArtUrl) %>
� � <%: Html.TextAreaFor(model => model.AlbumArtUrl) %>
� � <%: Html.ValidationMessageFor(model => model.AlbumArtUrl) %>
</p>

<p>
� � <%: Html.LabelFor(model => model.Artist) %>
� � <%: Html.DropDownList("ArtistId", new SelectList(ViewData["Artists"] as IEnumerable, "ArtistId", "Name", Model.ArtistId)) %>
</p>

<p>
� � <%: Html.LabelFor(model => model.Genre) %>
� � <%: Html.DropDownList("GenreId", new SelectList(ViewData["Genres"] as IEnumerable, "GenreId", "Name", Model.GenreId)) %>
</p>

� � <div>
� � � � <%: Html.ActionLink("Back to List", "Index") %>
� � </div>

 

This ascx file is contained within an Edit.aspx file:

 

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MvcMovies1.ViewModels.StoreManagerViewModel>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
� � Edit
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<form id="form1" runat="server">
� � <h2>Edit</h2>
� � <% using (Html.BeginForm())
� � � �{ %>
� � � <%: Html.ValidationSummary(true)%>

� � <fieldset>
� � � <legend>Edit Album</legend>
� � � <%: Html.EditorFor(model => model.Album,
� � � � � new { Artists = Model.Artists, Genres = Model.Genres }) %>

� � � � � <p><input type="submit" value="Save" /></p>


� � </fieldset>

� � � <% } %>
� � � </form>
</asp:Content>

 

 

I realise there's a lot of code there but if anyone can see something obvious that I am doing wrong I'd be very grateful.

Oct 21, 2010 at 5:28 PM

The tutorial keeps referring to an album with a id of 5, but there are no albums with that id - this must be from a previous tutorial. The form fails because there is no album with id = 5.

Run your project and in the url just type /StoreManager and hit return. Then on the list that appears, click the Edit link on one of the items. That will plug in a real album id and open your edit page.

The album ids are 3-digit numbers.

Coordinator
Oct 26, 2010 at 12:28 AM

Thanks, Wayne. I reset ID numbers for Artists but not for Albums. I'll get that on the next update.

Coordinator
Oct 26, 2010 at 12:29 AM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.