title: Why I get System.NullReferenceException: Object reference not set to an instance of an object?
body: <p>I have two models: Order and OrderDetails. Their models:</p>
  
  <pre><code>public class Order
      {   
          public int Id { get; set; }
          public int OrderDetailsId { get; set; }
          public string UserId { get; set; }
          public ApplicationUser User { get; set; }
          public ICollection&lt;OrderDetail&gt; OrderDetails { get; set; }
      }
  </code></pre>
  
  <p>and:</p>
  
  <pre><code>public class OrderDetail
      {
          public int Id { get; set; }
          public int OrderId { get; set; }
          public Order Order { get; set; }
          public int MenuId { get; set; }
          public Menu Menu { get; set; }
      }
  </code></pre>
  
  <p>And in the Details View of Order I wanted to show related OrderDetails. Details ActionResult:</p>
  
  <pre><code>public ActionResult Details(int? id)
          {
              if (id == null)
              {
                  return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
              }
              Order order = db.Orders.Where(o =&gt; o.Id == id).Include(o =&gt; o.OrderDetails).SingleOrDefault();
              //Order order = db.Orders.Where(o =&gt; o.Id == id).Include(o =&gt; o.OrderDetails).G;
              if (order == null)
              {
                  return HttpNotFound();
              }
              return View(order);
          }
  </code></pre>
  
  <p>And its view:</p>
  
  <pre><code>@model RestaurantApp.Models.Order
  
  &lt;div&gt;
      &lt;hr /&gt;
      &lt;dl class="dl-horizontal"&gt;
          &lt;dt&gt;
              @Html.DisplayNameFor(model =&gt; model.User.Email)
          &lt;/dt&gt;
  
          &lt;dd&gt;
              @Html.DisplayFor(model =&gt; model.User.Email)
          &lt;/dd&gt;
      &lt;/dl&gt;
  &lt;/div&gt;
  
  
  &lt;table class="table"&gt;
      @if (Model.OrderDetails == null)
      {
          &lt;tr&gt;no items found&lt;/tr&gt;
      }
      else
      {
          foreach (var item in Model.OrderDetails)
          {
              &lt;tr&gt;
                  &lt;td&gt;@item.MenuId&lt;/td&gt;
                  &lt;td&gt;@item.Quantity&lt;/td&gt;
                  &lt;td&gt;@item.Price&lt;/td&gt;
              &lt;/tr&gt;
          }
      }
  &lt;/table&gt;
  </code></pre>
  
  <p>And the problem is that I wanted to display Menu.Name instead of MenuId. But when I use Menu.Name it shows System.NullReferenceException: Object reference not set to an instance of an object. How can I solve this problem?</p>