title: Object Reference not set to an instance of object in asp.net mvc
body: <p>I'm creating new product belonging to the category model.When i execute my Create method, the error invoke that</p>
  
  <blockquote>
    <p>"Object Reference not set to instance of object" on "model" object</p>
  </blockquote>
  
  <p>My Controller class:</p>
  
  <pre><code>public ActionResult CreateProduct()   
  {
      SetCateProductViewBag();
      return View(new CateProdViewModel());
  }
  
  [HttpPost]       
  public ActionResult CreateProduct(CateProdViewModel model)
  {
  
      var ValidImageTypes = new String[]
      {
          "image/gif",
          "image/jpeg",
          "image/jpg",
          "image/pjpeg",
          "image/png"
      };
      if (model.ImageUpload == null || model.ImageUpload.ContentLength == 0)
      {
          ModelState.AddModelError("ImageUpload", "This Field is required.");
      }
      else if (!ValidImageTypes.Contains(model.ImageUpload.ContentType))
      {
          ModelState.AddModelError("ImageUload", "Please choose either a GIF,jpg or png type of file.");
      }
      if (ModelState.IsValid)
      {
  
          var prod = new Product
          {
              //   CategoryName =model.category.CategoryName,
              //CategoryDescription=model.category.CategoryDescription,
  
              //CategoryId=model.CategoryId,
              ProductName = model.ProductName,
              ProductDescription = model.ProductDescription,
              Model = model.Model,
              ProductPrice = model.ProductPrice,
              AvailableForSale = model.AvailableForSale,
              Shippable = model.Shippable,
              AvailableStock = model.AvailableStock,
              ProductPicture = model.ProductPicture
  
          };
          SetCateProductViewBag(prod.CategoryId);
          if (model.ImageUpload != null &amp;&amp; model.ImageUpload.ContentLength &gt; 0)
          {
              var UploadDir = "~/Uploads";
              var ImagePath = Path.Combine(Server.MapPath(UploadDir), model.ImageUpload.FileName);
              var ImageUrl = Path.Combine(UploadDir, model.ImageUpload.FileName);
              model.ImageUpload.SaveAs(ImagePath);
              prod.ProductPicture = ImageUrl;
          }
          productContext.product.Add(prod);
          productContext.SaveChanges();
          return RedirectToAction("CategoryIndex");
      }
  
      return View(model);
  }
  </code></pre>
  
  <p>CateProdViewModel class:</p>
  
  <pre><code>public class CateProdViewModel
  {
      public int CategoryId { get; set; }
      public string CategoryName { get; set; }
      public string CategoryDescription { get; set; }
      public string ProductName { get; set; }
      public string ProductDescription { get; set; }
      public int AvailableStock { get; set; }
      public decimal ProductPrice { get; set; }
      public string Model { get; set; }
      public bool AvailableForSale { get; set; }
      public bool Shippable { get; set; }
      [DataType(DataType.ImageUrl)]
      public string ProductPicture { get; set; }
      public int SelectedValue { get; set; }
      [DataType(DataType.Upload)]
      public HttpPostedFileBase ImageUpload { get; set; }
      public virtual Category category { get; set; }
      [Display(Name="Product Categories")]
      public virtual ICollection&lt;Category&gt; categories { get; set; }
  }
  </code></pre>
  
  <p>Entity classes for Category and Product:</p>
  
  <pre><code>public class Product
  {
      public int ProductId { get; set; }
      public string ProductName { get; set; }
      public string ProductDescription { get; set; }
      public int AvailableStock { get; set; }
      public decimal ProductPrice { get; set; }
      public string Model { get; set; }
      public bool AvailableForSale { get; set; }
      public bool Shippable { get; set; }
      public string ProductPicture { get; set; }
      public int CustomerId { get; set; }
      public int CategoryId { get; set; }
      public virtual Category category { get; set; }
  
  }
  
   public class Category
  {
      public int CategoryId { get; set; }
      public string CategoryName { get; set; }
      public string CategoryDescription { get; set; }
      public virtual ICollection&lt;Product&gt; product { get; set; }
  
  }
  </code></pre>
  
  <p>My View:</p>
  
  <pre><code>@model SmartShoppingCart.Models.CateProdViewModel
  
  @{
    ViewBag.Title = "CreateProduct";
  }
  
  &lt;h2&gt;Create Product&lt;/h2&gt;
  
  &lt;form action="" method="post" enctype="multipart/form-data"&gt;
      &lt;div&gt;
          @Html.LabelFor(m=&gt;m.CategoryId,"Category")
          @Html.DropDownList("CategoryId",ViewBag.categories as SelectList, string.Empty)
          @Html.ValidationMessageFor(m=&gt;m.CategoryId)
      &lt;/div&gt;
      &lt;div&gt;
          @Html.HiddenFor(m=&gt;m.CategoryId)
      &lt;/div&gt;
      &lt;div&gt;
          @Html.LabelFor(m=&gt;m.ProductName)
          @Html.TextBoxFor(m=&gt;m.ProductName)
          @Html.ValidationMessageFor(m=&gt;m.ProductName)
      &lt;/div&gt;
       &lt;div&gt;
          @Html.LabelFor(m=&gt;m.ProductDescription)
          @Html.TextBoxFor(m=&gt;m.ProductDescription)
          @Html.ValidationMessageFor(m=&gt;m.ProductDescription)
      &lt;/div&gt;
       &lt;div&gt;
          @Html.LabelFor(m=&gt;m.Model)
          @Html.TextBoxFor(m=&gt;m.Model)
          @Html.ValidationMessageFor(m=&gt;m.Model)
      &lt;/div&gt;
      &lt;div&gt;
          @Html.LabelFor(m=&gt;m.ProductPrice)
          @Html.TextBoxFor(m=&gt;m.ProductPrice)
          @Html.ValidationMessageFor(m=&gt;m.ProductPrice)
      &lt;/div&gt;
       &lt;div&gt;
          @Html.LabelFor(m=&gt;m.AvailableForSale)
          @Html.TextBoxFor(m=&gt;m.AvailableForSale)
          @Html.ValidationMessageFor(m=&gt;m.AvailableForSale)
      &lt;/div&gt;
       &lt;div&gt;
          @Html.LabelFor(m=&gt;m.Shippable)
          @Html.TextBoxFor(m=&gt;m.Shippable)
          @Html.ValidationMessageFor(m=&gt;m.Shippable)
      &lt;/div&gt;
       &lt;div&gt;
          @Html.LabelFor(m=&gt;m.AvailableStock)
          @Html.TextBoxFor(m=&gt;m.AvailableStock)
          @Html.ValidationMessageFor(m=&gt;m.AvailableStock)
      &lt;/div&gt;
      &lt;div&gt;
          @Html.LabelFor(m=&gt;m.ImageUpload)
          @Html.TextBoxFor(m =&gt; m.ImageUpload, new {type="file" })
      &lt;/div&gt;
      &lt;div&gt;
           &lt;button type="submit" value="Add" &gt;&lt;/button&gt;
      &lt;/div&gt;
  &lt;/form&gt;
  
  private void SetCateProductViewBag(int? CateId = null)
  {
      if (CateId == null)
      {
          ViewBag.Categories = new SelectList(productContext.category, "CategoryId", "CategoryName");
      }
      else
      {
          ViewBag.Categories = new SelectList(productContext.category.ToArray(),"CategoryId","CategoryName",CateId);
      }
  }
  </code></pre>