title: MVC Error: Object reference not set to an instance of an object
body: <p>I'm close to giving up on this mvc app for today!!</p>
  
  <p>I'm following the Mvc Music Store Tutorial and I'm stuck on page 54. </p>
  
  <p>this is the error I'm getting:</p>
  
  <blockquote>
    <p>System.NullReferenceException: Object
    reference not set to an instance of an
    object.</p>
  </blockquote>
  
  <p>The error occurs in the third paragraph block (dropdownlist) in the following code:</p>
  
  <pre><code>&lt;%@ Import Namespace ="MvcMovies1" %&gt;
  &lt;%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl&lt;MvcMovies1.Models.Album&gt;" %&gt;
  
  &lt;p&gt;
      &lt;%: Html.LabelFor(model =&gt; model.Title) %&gt;
      &lt;%: Html.TextAreaFor(model =&gt; model.Title) %&gt;
      &lt;%: Html.ValidationMessageFor(model =&gt; model.Title) %&gt;
  &lt;/p&gt;
  
  &lt;p&gt;
      &lt;%: Html.LabelFor(model =&gt; model.Price) %&gt;
      &lt;%: Html.TextAreaFor(model =&gt; model.Price) %&gt;
      &lt;%: Html.ValidationMessageFor(model =&gt; model.Price) %&gt;
  &lt;/p&gt;
  
  &lt;p&gt;
      &lt;%: Html.LabelFor(model =&gt; model.AlbumArtUrl) %&gt;
      &lt;%: Html.TextAreaFor(model =&gt; model.AlbumArtUrl) %&gt;
      &lt;%: Html.ValidationMessageFor(model =&gt; model.AlbumArtUrl) %&gt;
  &lt;/p&gt;
  
  &lt;p&gt;
      &lt;%: Html.LabelFor(model =&gt; model.Artist) %&gt;
      &lt;%: Html.DropDownList("ArtistId", new SelectList(ViewData["Artists"] as IEnumerable, "ArtistId", "Name", Model.ArtistId)) %&gt;
  &lt;/p&gt;
  
  &lt;p&gt;
      &lt;%: Html.LabelFor(model =&gt; model.Genre) %&gt;
      &lt;%: Html.DropDownList("GenreId", new SelectList(ViewData["Genres"] as IEnumerable, "GenreId", "Name", Model.GenreId)) %&gt;
  &lt;/p&gt;
  
      &lt;div&gt;
          &lt;%: Html.ActionLink("Back to List", "Index") %&gt;
      &lt;/div&gt;
  </code></pre>
  
  <p>This ascx file is contained within an Edit.aspx file:</p>
  
  <pre><code>&lt;%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage&lt;MvcMovies1.ViewModels.StoreManagerViewModel&gt;" %&gt;
  &lt;asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"&gt;
      Edit
  &lt;/asp:Content&gt;
  
  &lt;asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"&gt;
  &lt;form id="form1" runat="server"&gt;
      &lt;h2&gt;Edit&lt;/h2&gt;
      &lt;% using (Html.BeginForm())
         { %&gt;
        &lt;%: Html.ValidationSummary(true)%&gt;
  
      &lt;fieldset&gt;
        &lt;legend&gt;Edit Album&lt;/legend&gt;
        &lt;%: Html.EditorFor(model =&gt; model.Album,
            new { Artists = Model.Artists, Genres = Model.Genres }) %&gt;
  
            &lt;p&gt;&lt;input type="submit" value="Save" /&gt;&lt;/p&gt;
  
  
      &lt;/fieldset&gt;
  
        &lt;% } %&gt;
        &lt;/form&gt;
  &lt;/asp:Content&gt;
  </code></pre>
  
  <p>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.</p>
  
  <p>EDIT</p>
  
  <p>StoreManagerController.cs (Edit)</p>
  
  <pre><code> public ActionResult Edit(int id)
      {
          var viewModel = new StoreManagerViewModel
          {
              Album = storeDB.Albums.SingleOrDefault(a =&gt; a.AlbumId == id),
              Genres = storeDB.Genres.ToList(),
              Artists = storeDB.Artists.ToList()
          };
  
          return View(viewModel);
      }
  </code></pre>
  
  <p>Andddd..StoreManagerViewModel.cs</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using MvcMovies1.Models;
  
  namespace MvcMovies1.ViewModels
  {
      public class StoreManagerViewModel
      {
          public Album Album { get; set; }
          public List&lt;Artist&gt; Artists { get; set; }
          public List&lt;Genre&gt; Genres { get; set; }
      }
  }
  </code></pre>
  
  <p>Again I realise i called it MvcMovies1, this was a typo but everything is marked up accordingly.</p>