title: Object reference not set to an instance of an object in Razor View
body: <p>I have 2 entities, namely <strong>Product</strong> and <strong>Category</strong>,</p>
  
  <pre><code>public class Category
  {
      public int CategoryId { get; set; }
      public string CategoryName { get; set; }
      public ICollection&lt;Product&gt; Products { get; set; }  
  }
  
  public class Product
  {
      public int ProductId { get; set; }
      public string Name { get; set; }
      public decimal Price { get; set; }
      public int CategoryId { get; set; }
      public Category Category { get; set; }
  }
  </code></pre>
  
  <p><strong>Product Controller</strong></p>
  
  <pre><code>public class ProductsController : Controller
      {
          private DemoContext db = new DemoContext();
  
          // GET: Products
          public ActionResult Index()
          {
              var products = db.Products.Include(p =&gt; p.Category);
              return View(products.ToList());
          }
  
          // GET: Products/Details/5
          public ActionResult Details(int? id)
          {
              if (id == null)
              {
                  return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
              }
  
              Product product = db.Products.Find(id);
              if (product == null)
              {
                  return HttpNotFound();
              }
              return View(product);
          }
      }
  </code></pre>
  
  <p>I try to access the CategoryName Property from Category Entity in the Product Details View as this</p>
  
  <pre><code>&lt;h2&gt;@Model.Name&lt;/h2&gt;
  &lt;label&gt;@Model.Category.CategoryName&lt;/label&gt;
  &lt;li&gt;@Model.Price.ToString("##,###")&lt;/li&gt;
  </code></pre>
  
  <p>But I got error <strong>"Object reference not set to an instance of an object."</strong></p>
  
  <p>@Model.Category.CategoryName</p>