title: Why is "Object reference not set to an instance of an object." error being raised here?
body: <p>I am working on an asp.net mvc web application and i have the folloiwng view:-</p>
  
  <pre><code>@model MvcApplication4.Models.SelectedCustomers
  &lt;h3&gt;Select Customers Detials&lt;/h3&gt;
  @using (Html.BeginForm("Export", null))
  {    &lt;table&gt;
          &lt;tr&gt;
              &lt;th&gt;
                  NAME @Html.CheckBox("IncludeName", true)
              &lt;/th&gt;
              &lt;th&gt;
                  Description @Html.CheckBox("IncludeDescription", true)
              &lt;/th&gt;
              &lt;th&gt;
                  Address @Html.CheckBox("IncludeAddress", true)
              &lt;/th&gt;
          &lt;/tr&gt;
          @foreach (var item in Model.Info) {
              &lt;tr&gt;
                  &lt;td&gt;
                      @Html.DisplayFor(modelItem =&gt; item.ORG_NAME)
                  &lt;/td&gt;
                  &lt;td&gt;
                      @Html.DisplayFor(modelItem =&gt; item.LOGIN_URI)
                  &lt;/td&gt;
                  &lt;td&gt;
                      @Html.DisplayFor(modelItem =&gt; item.SUPPORT_EMAIL)
                  &lt;/td&gt;
                  @Html.HiddenFor(modelItem =&gt; item.ORG_ID)
              &lt;/tr&gt;
          }
      &lt;/table&gt;
      &lt;p&gt;
          @Html.ActionLink("Back","customer","Home") |
          &lt;button type="submit" name="Format" value="xls"&gt;extract to excel&lt;/button&gt; |
          &lt;button type="submit" name="Format" value="csv"&gt;extract to text file&lt;/button&gt;
      &lt;/p&gt;
  }
  </code></pre>
  
  <p>and the folloiwng action methods:-</p>
  
  <pre><code>[HttpPost]
          public ActionResult Export(ExportViewModel exportOptions, IList&lt;AccountDefinition&gt; accounts)
          {
            // var accounts = GetAccount();
  
              if (exportOptions.Format == "csv")
              {
                  return accounts.AsCsvResult(exportOptions);
              }
              else if (exportOptions.Format == "xls")
              {
                  return accounts.AsXlsResult(exportOptions);
              }
  
              throw new NotSupportedException(
                  string.Format("Unsupported format: {0}", exportOptions.Format)
              );
          }
  </code></pre>
  
  <p>Then i defined the folloiwng method inside my AccountDefinition model:-</p>
  
  <pre><code> public partial class AccountDefinition
      {
          public long ORG_ID { get; set; }
          public string ORG_NAME { get; set; }
          public string LOGIN_URI { get; set; }
          public string SUPPORT_EMAIL { get; set; }
  
          public virtual SDOrganization SDOrganization { get; set; }
  
      }
      public static class ActionResultextensions
      {
          public static ActionResult AsCsvResult(this IEnumerable&lt;AccountDefinition&gt; accounts, ExportViewModel exportOptions)
          {
              return new CsvResult(accounts, exportOptions);
          }
  
          public static ActionResult AsXlsResult(this IEnumerable&lt;AccountDefinition&gt; accounts, ExportViewModel exportOptions)
          {
              return new XlsResult(accounts, exportOptions);
          }
      }
  }
  </code></pre>
  
  <p>Then i have the folloiwng model classes:-</p>
  
  <p>1.</p>
  
  <pre><code>public class CsvResult : ExportAccountsResult
      {
          public CsvResult(IEnumerable&lt;AccountDefinition&gt; accounts, ExportViewModel exportOptions)
              : base(accounts, exportOptions)
          {
          }
  
          protected override string ContentType
          {
              get { return "text/csv"; }
          }
  
          protected override string Filename
          {
              get { return "accounts.csv"; }
          }        }
  </code></pre>
  
  <p>2,</p>
  
  <pre><code> public class XlsResult : ExportAccountsResult
      {
          public XlsResult(IEnumerable&lt;AccountDefinition&gt; accounts, ExportViewModel exportOptions)
              : base(accounts, exportOptions)
          {
          }
  
          protected override string ContentType
          {
              get { return "application/vnd.ms-excel"; }
          }
  
          protected override string Filename
          {
              get { return "accounts.csv"; }
          }
      }
  </code></pre>
  
  <p>3.</p>
  
  <pre><code>public class ExportViewModel
      {
          public string Format { get; set; }
          public bool IncludeName { get; set; }
          public bool IncludeDescription { get; set; }
          public bool IncludeAddress { get; set; }
  
      }
  </code></pre>
  
  <p>4.</p>
  
  <pre><code>public abstract class ExportAccountsResult : ActionResult
      {
          protected ExportAccountsResult(IEnumerable&lt;AccountDefinition&gt; accounts, ExportViewModel exportOptions)
          {
              this.Accounts = accounts;
              this.ExportOptions = exportOptions;
          }
  
          protected IEnumerable&lt;AccountDefinition&gt; Accounts { get; private set; }
          protected ExportViewModel ExportOptions { get; private set; }
  
          protected abstract string ContentType { get; }
          protected abstract string Filename { get; }
  
          public override void ExecuteResult(ControllerContext context)
          {
              var response = context.HttpContext.Response;
              response.ContentType = ContentType;
              var cd = new ContentDisposition
              {
                  FileName = this.Filename,
                  Inline = false
              };
              response.AddHeader("Content-Disposition", cd.ToString());
  
              // TODO: Use a real CSV parser here such as https://github.com/JoshClose/CsvHelper/wiki/Basics
              // and never roll your own parser as shown in this oversimplified
              // example. Here's why: http://secretgeek.net/csv_trouble.asp
              using (var writer = new StreamWriter(response.OutputStream))
              {
                  foreach (var account in this.Accounts)
                  {
                      var values = new List&lt;object&gt;();
                      if (this.ExportOptions.IncludeName)
                      {
                          values.Add(account.ORG_NAME);
                      }
                      if (this.ExportOptions.IncludeDescription)
                      {
                          values.Add(account.LOGIN_URI);
                      }
                      if (this.ExportOptions.IncludeAddress)
                      {
                          values.Add(account.SUPPORT_EMAIL);
                      }
                      writer.WriteLine(string.Join(", ", values));
                  }
              }
          }
      }
  </code></pre>
  
  <p>But when i run click on "extract to excel" or "extractt to text file" button inside the view i got the following exception :-</p>
  
  <pre><code>System.NullReferenceException was unhandled by user code
    HResult=-2147467261
    Message=Object reference not set to an instance of an object.
    Source=MvcApplication4
    StackTrace:
         at MvcApplication4.Models.ExportAccountsResult.ExecuteResult(ControllerContext context) in c:\Users\Administrator\Desktop\MvcApplication4\MvcApplication4\Models\ExportAccountsResult.cs:line 41
         at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult)
         at System.Web.Mvc.ControllerActionInvoker.&lt;&gt;c__DisplayClass1c.&lt;InvokeActionResultWithFilters&gt;b__19()
         at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)
    InnerException:
  </code></pre>
  
  <p>on the following code inside the ExportAccountsresults.cs:-</p>
  
  <pre><code> foreach (var account in this.Accounts)
                  {
  </code></pre>
  
  <p><strong>:::UPDATED:::</strong></p>
  
  <p>I have updated my view to the following:-</p>
  
  <pre><code>@model MvcApplication4.Models.SelectedCustomers
  
  
  
  &lt;h3&gt;Select Customers Detials&lt;/h3&gt;
  
      @using (Html.BeginForm("Export", null))
      {
          Int32 c = -1;
          &lt;table&gt;
              &lt;tr&gt;
                  &lt;th&gt;
                      NAME @Html.CheckBox("IncludeName", true)
                  &lt;/th&gt;
                  &lt;th&gt;
                      Description @Html.CheckBox("IncludeDescription", true)
                  &lt;/th&gt;
                  &lt;th&gt;
                      Address @Html.CheckBox("IncludeAddress", true)
                  &lt;/th&gt;
              &lt;/tr&gt;
              @foreach (var item in Model.Info) {
                  c++;
                  &lt;tr&gt;
                      &lt;td&gt;
                          @Html.DisplayFor(modelItem =&gt; item.ORG_NAME)
                      &lt;/td&gt;
                      &lt;td&gt;
                          @Html.DisplayFor(modelItem =&gt; item.LOGIN_URI)
                      &lt;/td&gt;
                      &lt;td&gt;
                          @Html.DisplayFor(modelItem =&gt; item.SUPPORT_EMAIL)
                      &lt;/td&gt;
  
                       &lt;td&gt;@Html.Hidden(String.Format("Info[{0}].ORG_ID", c), item.ORG_ID)&lt;/td&gt;
                  &lt;/tr&gt;
              }
          &lt;/table&gt;
  
          &lt;p&gt;
              @Html.ActionLink("Back","customer","Home") |
              &lt;button type="submit" name="Format" value="xls"&gt;extract to excel&lt;/button&gt; |
              &lt;button type="submit" name="Format" value="csv"&gt;extract to text file&lt;/button&gt;
          &lt;/p&gt;
      }
  </code></pre>
  
  <p>And my action method to the following:-</p>
  
  <pre><code>  [HttpPost]
          public ActionResult Export(ExportViewModel exportOptions, SelectedCustomers sc)
          {
            // var accounts = GetAccount();
             var accounts = sc.Info.ToList();
              if (exportOptions.Format == "csv")
              {
                  return accounts.AsCsvResult(exportOptions);
              }
              else if (exportOptions.Format == "xls")
              {
                  return accounts.AsXlsResult(exportOptions);
              }
  
              throw new NotSupportedException(
                  string.Format("Unsupported format: {0}", exportOptions.Format)
              );
          }
  </code></pre>
  
  <p>Now the csv file will open but it will be empty and it will noly contain ", ," for each record, for exmaple if there should be 7 records in the excel sheet or text file then the .csv file will have the following without data:-</p>
  
  <pre><code>, , 
  , , 
  , , 
  , , 
  , , 
  , , 
  , , 
  </code></pre>