title: 'Object reference not set to an instance of an object' - what does this mean?
body: <p>I am making an MVC 3 app which contains data about "associates", however I also need to be able to delete these "associates" through the app. I am using the Delete ActionResult but every time I try to delete an entry I get the following server error:</p>
  
  <pre><code>Object reference not set to an instance of an object.
  
  Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
  
  Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
  
  Source Error: 
  
  
  Line 14:         &lt;div class="form-grid-1"&gt;
  Line 15:             &lt;div class="display-label"&gt;@Html.LabelFor(model =&gt; model.FirstName)&lt;/div&gt;
  Line 16:             &lt;div class="display-field"&gt;@Model.FirstName&lt;/div&gt;
  Line 17:         &lt;/div&gt;
  Line 18: 
  
  Source File: c:\Users\Jackie\Documents\Visual Studio 2010\Projects\Associate Tracker versions\Associate Tracker v5\LoginFormExample\Views\Associate\Delete.cshtml    Line: 16 
  
  Stack Trace: 
  
  
  [NullReferenceException: Object reference not set to an instance of an object.]
     ASP._Page_Views_Associate_Delete_cshtml.Execute() in c:\Users\Jaskharan Shoker\Documents\Visual Studio 2010\Projects\Associate Tracker versions\Associate Tracker v5\LoginFormExample\Views\Associate\Delete.cshtml:16
     System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +207
     System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +81
     System.Web.WebPages.StartPage.RunPage() +19
     System.Web.WebPages.StartPage.ExecutePageHierarchy() +65
     System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +76
     System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +220
     System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +115
     System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +303
     System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +13
     System.Web.Mvc.&lt;&gt;c__DisplayClass1c.&lt;InvokeActionResultWithFilters&gt;b__19() +23
     System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +260
     System.Web.Mvc.&lt;&gt;c__DisplayClass1e.&lt;InvokeActionResultWithFilters&gt;b__1b() +19
     System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +177
     System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +343
     System.Web.Mvc.Controller.ExecuteCore() +116
     System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +97
     System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +10
     System.Web.Mvc.&lt;&gt;c__DisplayClassb.&lt;BeginProcessRequest&gt;b__5() +37
     System.Web.Mvc.Async.&lt;&gt;c__DisplayClass1.&lt;MakeVoidDelegate&gt;b__0() +21
     System.Web.Mvc.Async.&lt;&gt;c__DisplayClass8`1.&lt;BeginSynchronous&gt;b__7(IAsyncResult _) +12
     System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
     System.Web.Mvc.&lt;&gt;c__DisplayClasse.&lt;EndProcessRequest&gt;b__d() +50
     System.Web.Mvc.SecurityUtil.&lt;GetCallInAppTrustThunk&gt;b__0(Action f) +7
     System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22
     System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +60
     System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
     System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8969117
     System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&amp; completedSynchronously) +184
  </code></pre>
  
  <p><strong>The controller:</strong></p>
  
  <pre><code>//
  // GET: /Associate/Delete/AssociateId
  public ActionResult Delete(int id)
  {
      using (var db = new AssociateDBEntities())
      {
          return View(db.Associates.Find(id));
          }
      }
  
  //
  // POST: /Associate/Delete/AssociateId
  [HttpPost]
  public ActionResult Delete(int id, Associate associate)
  {
          try
          {
              using (var db = new AssociateDBEntities())
              {
                  db.Entry(associate).State = System.Data.EntityState.Deleted;
                  db.SaveChanges();
              }
              return RedirectToAction("ViewAll");
          }
          catch
          {
              return View();
          }
  </code></pre>
  
  <p><strong>'Delete' view:</strong></p>
  
  <pre><code>@model LoginFormExample.Models.Associate
  
  @{
      ViewBag.Title = "Delete";
  }
  
  &lt;h2&gt;Delete&lt;/h2&gt;
  &lt;h3&gt;Are you sure you want to delete this associate?&lt;/h3&gt;
  &lt;fieldset&gt;
      &lt;legend&gt;Associate&lt;/legend&gt;
  
          @Html.HiddenFor(model =&gt; model.AssociateId)
  
          &lt;div class="form-grid-1"&gt;
              &lt;div class="display-label"&gt;@Html.LabelFor(model =&gt; model.FirstName)&lt;/div&gt;
              &lt;div class="display-field"&gt;@Model.FirstName&lt;/div&gt;
          &lt;/div&gt;
  
          &lt;div class="form-grid-2"&gt;
              &lt;div class="display-label"&gt;@Html.LabelFor(model =&gt; model.LastName)&lt;/div&gt;
              &lt;div class="display-field"&gt;@Model.LastName&lt;/div&gt;
          &lt;/div&gt;
  
          &lt;div class="form-grid-1"&gt;
               &lt;div class="display-label"&gt;@Html.LabelFor(model =&gt; model.Email)&lt;/div&gt;
              &lt;div class="display-field"&gt;@Model.Email&lt;/div&gt;
          &lt;/div&gt;
  
          &lt;div class="form-grid-2"&gt;
              &lt;div class="display-label"&gt;@Html.LabelFor(model =&gt; model.AddressLine1)     &lt;/div&gt;
              &lt;div class="display-field"&gt;@Model.AddressLine1&lt;/div&gt;
          &lt;/div&gt;
  
          &lt;div class="form-grid-1"&gt;
              &lt;div class="display-label"&gt;@Html.LabelFor(model =&gt; model.AddressLine2)&lt;/div&gt;
              &lt;div class="display-field"&gt;@Model.AddressLine2&lt;/div&gt;
          &lt;/div&gt;
  
          &lt;div class="form-grid-2"&gt;
              &lt;div class="display-label"&gt;@Html.LabelFor(model =&gt; model.Postcode)&lt;/div&gt;
              &lt;div class="display-field"&gt;@Model.Postcode&lt;/div&gt;
          &lt;/div&gt;
  
          &lt;div class="form-grid-1"&gt;
              &lt;div class="display-label"&gt;@Html.LabelFor(model =&gt; model.Phone)&lt;/div&gt;
              &lt;div class="display-field"&gt;@Model.Phone&lt;/div&gt;
          &lt;/div&gt;
  
          &lt;div class="form-grid-2"&gt;
              &lt;div class="display-label"&gt;@Html.LabelFor(model =&gt; model.Mobile)&lt;/div&gt;
              &lt;div class="display-field"&gt;@Model.Mobile&lt;/div&gt;
          &lt;/div&gt;
          &lt;br /&gt;
  &lt;/fieldset&gt;
  
  @using (Html.BeginForm()) {
      &lt;p&gt;
          &lt;input type="submit" value="Delete" /&gt; |
          @Html.ActionLink("Back to view all", "ViewAll")
      &lt;/p&gt;
  }
  </code></pre>
  
  <p><strong>'Associate' class</strong></p>
  
  <pre><code>namespace LoginFormExample.Models
  {
      public partial class Associate
      {
          public int AssociateId { get; set; }
  
          [Required]
          [Display(Name = "First name:")]
          public string FirstName { get; set; }
  
          [Required]
          [Display(Name = "Last name:")]
          public string LastName { get; set; }
  
          [Required(ErrorMessage = "Please enter a valid email address")]
          [DataType(DataType.EmailAddress)]
          [Display(Name = "Email address:")]
          public string Email { get; set; }
  
          [Display(Name = "Address line 1:")]
          public string AddressLine1 { get; set; }
  
          [Display(Name = "Address line 2:")]
          public string AddressLine2 { get; set; }
          public int RegionId { get; set; }
          public int CityTownId { get; set; }
  
          [Display(Name = "Postcode:")]
          public string Postcode { get; set; }
  
          [Display(Name = "Phone number:")]
          public string Phone { get; set; }
  
          [Display(Name = "Mobile number:")]
          public string Mobile { get; set; }
  </code></pre>
  
  <p>Does anybody have any ideas as to why I keep getting this error? I have checked against example applications and I can't seem to spot any differences.</p>