title: Object returning null after creating object
body: <p>I'm playing around with ASP.net, using a code first approach to create and use a database.</p>
  
  <p>Using the code below I get an exception of type 'System.NullReferenceException' Object reference not set to an instance of an object (see code for specific line). </p>
  
  <p>When I step through the application, the variable in question, 'p' has a value of {Code_first_database.Models.Product}, not null.</p>
  
  <p>I've looked at <a href="https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it%22What%20is%20a%20NullReferenceException%20and%20how%20do%20I%20fix%20it?%22">What is a NullReferenceException and how do I fix it?</a> and <a href="https://stackoverflow.com/questions/779091/what-does-object-reference-not-set-to-an-instance-of-an-object-mean">What does “Object reference not set to an instance of an object” mean?</a> but I still cant seem to see the problem.</p>
  
  <p>Any help much appreciated.</p>
  
  <p><strong>HomeController.cs</strong></p>
  
  <pre><code>namespace Code_first_database.Controllers
  {
      public class HomeController : Controller
      {
          public ActionResult Index()
          {
              Models.Product p = new Models.Product("Soft331", "Book");
              using (var db = new Models.ProductDB())
              {
                  db.products.Add(p); // **EXCEPTION THROWN HERE**
                  db.SaveChanges();
                  ViewBag.Message = "The number of products so far is " +
                      db.products.Count().ToString();
              }
  
              return View();
          }
      }
  }
  </code></pre>
  
  <p><strong>Product.cs</strong></p>
  
  <pre><code>namespace Code_first_database.Models
  {
      public class Product
      {
          public int Id { get; set; }
          public string Product_Code { get; set; }
          public string Product_Name { get; set; }
          public float Price { get; set; }
          public int Stock { get; set; }
  
          public Product(string p_code, string p_name)
          {
              Product_Code = p_code;
              Product_Name = p_name;
          }
      }
  }
  </code></pre>
  
  <p><strong>ProductDB.cs</strong></p>
  
  <pre><code>namespace Code_first_database.Models
  {
      public class ProductDB : DbContext
      {
          public DbSet&lt;Product&gt; products;
  
          public ProductDB() : base("DefaultConnection")
          {
  
          }
      }
  }
  </code></pre>
  
  <p>Thanks
  Tony</p>