MVC error: object reference not set to object instance

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

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

this is the error i get:

System.NullReferenceException: The object reference is not set to an instance of the object.

An error in the third paragraph block (dropdownlist) in the following code:

<%@ 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 in the 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 understand there is a lot of code, but if someone sees something obvious that I am doing wrong, I would be very grateful.

EDIT

StoreManagerController.cs (Change)

  public ActionResult Edit(int id) { var viewModel = new StoreManagerViewModel { Album = storeDB.Albums.SingleOrDefault(a => a.AlbumId == id), Genres = storeDB.Genres.ToList(), Artists = storeDB.Artists.ToList() }; return View(viewModel); } 

Andddd..StoreManagerViewModel.cs

 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<Artist> Artists { get; set; } public List<Genre> Genres { get; set; } } } 

Again, I understand that I called it MvcMovies1, it was a typo, but everything was marked accordingly.

+6
c # asp.net-mvc
source share
6 answers

Does Album ArtistId , since on this line you call Model.ArtistId , and if Album does not have this property, you will get a null-reference exception. This is because Model is a shorthand for an object that is strongly typed for your representation, which in your case is Album .

There is no place in your code above where you install ViewData ["Artists"]. You install it anywhere, as that too may be your problem.

EDIT

Set ViewData in action and it should work:

 public ActionResult Edit(int id) { var viewModel = new StoreManagerViewModel { Album = storeDB.Albums.SingleOrDefault(a => a.AlbumId == id), Genres = storeDB.Genres.ToList(), Artists = storeDB.Artists.ToList() }; ViewData["Artists"] = storeDB.Artists.ToList(); ViewData["Genres"] = storeDB.Genres.ToList(); return View(viewModel); } 
+7
source share

First you need to add properties in your view model to hold the selected artist and the selected genre:

 public class StoreManagerViewModel { public Album Album { get; set; } public int? SelectedArtistId { get; set; } public List<Artist> Artists { get; set; } public int? SelectedGenreId { get; set; } public List<Genre> Genres { get; set; } } 

Then in your view Edit.aspx instead:

 <%: Html.EditorFor(model => model.Album, new { Artists = Model.Artists, Genres = Model.Genres }) %> 

You could simply:

 <%: Html.EditorForModel() %> 

and in your editor template ~/Views/Home/EditorTemplates/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> 

and in the editor template ~/Views/Home/EditorTemplates/StoreManagerViewModel :

 <%@ Import Namespace ="MvcMovies1" %> <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MvcMovies1.ViewModels.StoreManagerViewModel>" %> <%: Html.EditorFor(model => model.Album) %> <p> <%: Html.LabelFor(model => model.SelectedArtistId) %> <%: Html.DropDownListFor(model => model.SelectedArtistId, new SelectList(Model.Artists, "ArtistId", "Name")) %> </p> <p> <%: Html.LabelFor(model => model.SelectedGenreId) %> <%: Html.DropDownListFor(model => model.SelectedGenreId, new SelectList(Model.Genres, "GenreId", "Name")) %> </p> <div> <%: Html.ActionLink("Back to List", "Index") %> </div> 
+6
source share

You simply do not set ViewData["Artists"] and ViewData["genres"] as I see from your action method.

Make sure you set these values โ€‹โ€‹somewhere during the term of the request if you want to use them in your view.

+1
source share

Have you tried listing List and List instead of IEnumerable?

+1
source share

problem:

Object reference not set to an instance of an object

Decision:

 pubblic ActionResult yourDetails() { List<YourEntityName> PropertyName= new List<YourEntityName>(); list = db.PropertyName.ToList(); return View(list); } 

YourEntityName means your table name, i.e. PassengerDetial

PropertyName means your public class property, i.e. PassengerDetails

 pubblic Class PassengerDetailContext : DbContext { pubblic DbSet<PassengerDetail> passegerDetails {get;set;} } List<PassengerDetail> passegerDetails = new List<PassengerDetail>(); list = db.passegerDetails.ToList(); return View(list); 
0
source share

The same error occurred while trying to migrate (update-database). has 2 projects, namely ViewProject and DBcontextProject.

my problem was that I made ViewProject a startup project and migrated to DBcontextProject.

The solution was to make DBcontextProject the starting project and start the migration to DBcontextProject.

0
source share

All Articles