0

I understand how this error is coming about, but I don't get why. The context makes no sense to me. Allow me to explain:

This is my "Customers/details.cshtml" View for a super simple page:

@model Vidly.Models.Customer

@{
    ViewBag.Title = Model.Name;
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>@Model.Name</h2>
<ul>
    <li>@Model.MembershipType.Name</li>
    @if (Model.Birthday.HasValue)
    {
    <li>@Model.Birthday.Value.ToShortDateString()</li>
    }
</ul>

The Line <li>@Model.MembershipType.Name</li> is creating the following error:

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

I get it, its telling me I need to create an instance to use this property... But why??? The thing that is confusing the heck out of me is that without that single line of code, everything else on the page works fine. Why is it that Model.MembershipType.Name requires an instance but Model.Name does not and Model.Birthday.Value.ToShortDateString() does not??? On my Customers/index.cshtml page, I have the following code block: Note my removal of unnecessary code noted by "...":

@model IEnumerable<Vidly.Models.Customer>

...

        @foreach (var customer in Model)
        {
            <tr>
                <td>@Html.ActionLink(customer.Name, "Details", "Customers", new {id = customer.Id}, null)</td>
                <td>@customer.MembershipType.Name</td>
            </tr>
        }

Obviously, this foreach is instantiating an object of the Customer Model and in fact this implementation of the MembershipType.Name is working just fine... But, I don't need to iterate over it in the Details page, so if it is absolutely necessary to create an object (I still don't see why), what is the best way to do it?

EDIT: Added Controller code:

public ActionResult Details(int id)
        {
           var customer = _context.Customers.SingleOrDefault(c => c.Id == id);

            if (customer == null)
                return HttpNotFound();

            return View(customer);
        }
the_endian
  • 2,259
  • 1
  • 24
  • 49
  • 1
    We need to see how you populate your Model object you pass to the view, if it tells you that it is null, it's null there is no secret – Hassan Jul 18 '16 at 20:09
  • 1
    My bet is that the Model.MembershipType is set to null and not populated in the details controller. – Ralf Bönning Jul 18 '16 at 20:10
  • 1
    Please provide the source code of your controller that is building your model, and update your question with that. It would be helpful to see how your model object is getting hydrated. – dvsoukup Jul 18 '16 at 20:11
  • The foreach is NOT instantiating an object of the type Customer Model, the foreach only iterates through an instantiation of IEnumerable that you send to the .cshtml view from your controller, you must me creating a List() in your controller – Brian Ogden Jul 18 '16 at 20:16
  • Not sure why this was closed, but for an FYI, your model needs to populate the MembershipType before you can access the Name property. – Christopher G. Lewis Jul 18 '16 at 20:21
  • @rboe @Massanu you are both right! I added this line of code: `Customers.Include(c => c.MembershipType)` to the controller code which solved the problem. @Win @Tim @S.Akbari I actually read the so-called "duplicate" but it really did not identify the exact problem in this particular situation _in a way that someone at my experience level could understand_. I am disappointed to see this closed as this is a slightly different case and could benefit future readers. I would rather at least be able to post the solution properly. – the_endian Jul 18 '16 at 20:29
  • 1
    @TeeSee it is duplicate because this post asks about NRE. It can't be found by future visitors for any other problem. If you feel that your question is different you need to make it different (and searchable by problem you are trying to solve). First step could be replace "I understand how this error is coming about" with "this is because XXXXXX is null, but it should not be as I set it in my code on line YYYYY". – Alexei Levenkov Jul 18 '16 at 20:44
  • @AlexeiLevenkov Thank you. So essentially I can engineer my question so that it more thoroughly explains its own uniqueness and therefore answers a separate or more specific question? – the_endian Jul 18 '16 at 21:21
  • @TeeSee yes. Alternatively you can *demonstrate research*. "Googled a lot", "I've seen other answers but none helped" and other generic phrases don't count as such. You need very concrete information about your particular problem what you've tried and what you've found. Or you can just be happy with comments + closure without downvotes :) – Alexei Levenkov Jul 18 '16 at 23:18
  • @AlexeiLevenkov Thanks for that info. Is there any one place I can find more insight into this? I've read the basic question-writing advice but more info on what this community expects would be helpful. Also, could you do me a favor and link me to an example of a particulary good question in your experience? Thank you. – the_endian Jul 18 '16 at 23:31

0 Answers0