title: I get an error as [ Object reference not set ....] when i try to submit a form to store some values and the path of an uploaded image into database
body: <p>I have a form to store some records in events table in database along with the path of an uploaded file. </p>
  
  <p>But when i click the Save button, it generates the following error:</p>
  
  <blockquote>
    <p>Object reference not set to an instance of an object.</p>
  </blockquote>
  
  <p>This is my model:</p>
  
  <pre><code>public class events1
  {
      public int Id { get; set; }
      public string title { get; set; }
      public string report { get; set; }
      public string image1 { get; set; }
      public string image2 { get; set; }
  
      [NotMapped]
      public HttpPostedFileBase ImageFile { get; set; }
  }
  </code></pre>
  
  <p>This is my controller</p>
  
  <pre><code>    public ActionResult Index()
      {
          var evt = _context.evt1;
          if (evt==null)
          {
              return Content("No events registered in database");
          }
          return View(evt);
      }
  
  
  
      public ActionResult Add_New()
      {
  
          return View();
      }
  
      [HttpPost]
      public ActionResult Save(events1 e)
      {
  
          string filename = Path.GetFileNameWithoutExtension(e.ImageFile.FileName);
          string extension = Path.GetExtension(e.ImageFile.FileName);
          filename = filename + DateTime.Now.ToString("yymmssfff") + extension;
          e.image1 = "~/Uploaded_Files/" + filename;
          filename = Path.Combine(Server.MapPath("~/Uploaded_Files/"), filename);
          e.ImageFile.SaveAs(filename);
  
  
  
          _context.evt1.Add(e);
          _context.SaveChanges();
          ModelState.Clear();
  
          return RedirectToAction("Index","Events1");
      }
  </code></pre>
  
  <p>And this is the view:</p>
  
  <pre><code>   @model UploadFile_Button.Models.events1
  @{
  ViewBag.Title = "Add_New";
  Layout = "~/Views/Shared/_Layout.cshtml";
   }
  
   @using (Html.BeginForm("Save", "Events1"))
  
   {
  &lt;div class="form-group"&gt;
      @Html.LabelFor(a =&gt; a.title)
      @Html.TextBoxFor(a =&gt; a.title, new { @class = "form-control" })
  &lt;/div&gt;
  &lt;div class="form-group"&gt;
      @Html.LabelFor(a =&gt; a.report)
      @Html.TextBoxFor(a =&gt; a.report, new { @class = "form-control" })
  &lt;/div&gt;
  &lt;div class="form-group"&gt;
     &lt;input type="file" name="ImageFile"/&gt;
  &lt;/div&gt;
  &lt;div class="form-group"&gt;
      @Html.LabelFor(a =&gt; a.image2)
      @Html.TextBoxFor(a =&gt; a.image2, new { @class = "form-control" })
  &lt;/div&gt;
  &lt;button type="submit" class="btn btn-primary"&gt;Save&lt;/button&gt;
  }
  </code></pre>
  
  <p>I have to mention when it generates the error, it also highlights this line of my code in RED color:</p>
  
  <blockquote>
  <pre><code>string filename = Path.GetFileNameWithoutExtension(e.ImageFile.FileName);
  </code></pre>
  </blockquote>