title: ASP.NET MVC - Object reference not set to an instance of an object
body: <p>I have recently taken over support of an ASP.NET MVC project, and am trying to work through some of the errors, one in particular has me stumped though.</p>
  
  <p>We have a 'New' page to add new items, with the following code running when the page is posted:</p>
  
  <pre><code>[HttpPost]
  public ActionResult New(RecordView i)
  {
      if(ModelState.IsValid)
      {
          repository.AddRecord(i.DogIncident);
  
          return View("Index");
      }
      return View("Index");
  }
  </code></pre>
  
  <p>However, when it tries to load the Index view, I get the following error: <em>"Object reference not set to an instance of an object."</em> and it points to the following block of code at the top of a file called RecordsView.cshtml:</p>
  
  <pre><code>@for (var i = 0; i &lt; Model.Records.Count; i++)
  </code></pre>
  
  <p>The record does add correctly though, it just doesn't load the listings page correctly, and since this is just a "nice to have" I thought I'd simplify things by changing it so that it either returns some text which generates an error as it's expecting a boolean returned.</p>
  
  <p>Any ideas on how to fix this please? I'm stumped.</p>
  
  <p>Thanks!</p>