title: "Object reference not set to an instance of an object" for List<T>.Add
body: <p>In my method I have multiple statements that are filtering data and returning and object. Because I want to serve the view with an view model I instantiate a list and then pass it into view...</p>
  
  <p>Simplified example.</p>
  
  <pre><code>    List&lt;viewModel&gt; returnedViewModel = new List&lt;viewModel&gt;();
  
      foreach (var item in filteredData)
      {
          returnedViewModel.Add( // this line is throwing error
          new viewModel
          {
              data = item,
          });
      } 
  </code></pre>
  
  <p>The issue is that I'm getting "Object reference not set to an instance of an object." for returnedViewModel.Add for one particular filteredData object while other are not causing that issue.</p>
  
  <p>I'm after a possible source of that problem not the solution.</p>
  
  <p>I'm using EF and have a MARS statement in my connectionString. Can that be an issue?</p>
  
  <p>Whole statment:</p>
  
  <pre><code>public ActionResult RenderPartialSearchableEventStacks(string viewType,
                                                         string orderColumn,
                                                         string sortOrder,
                                                         int? stackNumber,
                                                         string adminName,
                                                         string clientName,
                                                         string stackType,
                                                         string stackStatus,
                                                         int? adminId = null)
  {
      IEnumerable&lt;blsEventStack&gt; eventStacks = unitOfWork.EventStackRepository.Filter(n =&gt; (stackNumber == null) || n.EventStackId == stackNumber,
                                                                                      n =&gt; (String.IsNullOrEmpty(adminName)) || n.ManagingAdminName.Contains(adminName),
                                                                                      n =&gt; (String.IsNullOrEmpty(clientName)) || n.RelatedClientName.Contains(clientName),
                                                                                      n =&gt; (String.IsNullOrEmpty(stackType)) || n.EventStackType.Contains(stackType),
                                                                                      n =&gt; (String.IsNullOrEmpty(stackStatus)) || n.EventStackLastEventStatus.Contains(stackStatus));
  
      IEnumerable&lt;blsEventStack&gt; viewTypeRelatedEventStacks = eventStacks;
  
      if (viewType == "allEventStacks")
      {
          viewTypeRelatedEventStacks = unitOfWork.EventStackRepository.GetAll();
      }
  
      if (viewType == "assignedEventStacks")
      {
          // Needed to filter out results with empty (unassigned) admin name
          viewTypeRelatedEventStacks = from x in eventStacks
                                       where x.ManagingAdminName != null
                                       select x;
      }
  
      if (viewType == "singleAdminAssignedEventStacks")
      {
          string singleAdminName;
  
          if (adminId != null)
          {
              // Gets admin name using adminId
              singleAdminName = unitOfWork.AdminRepository.GetById(adminId).AdminName;
          }
          else
          {
              MembershipUser admin = Membership.GetUser();
  
              blsAdmin loggedInAdmin = (from x in unitOfWork.AdminRepository.GetAll()
                                        where x.AdminEmail == admin.Email
                                        select x).FirstOrDefault();
  
              singleAdminName = loggedInAdmin.AdminName;
  
              ViewBag.adminView = true;
          }
  
          // Gets all eventStacks that belong to a give adminName
          viewTypeRelatedEventStacks = from x in eventStacks
                                       where x.ManagingAdminName == singleAdminName
                                       select x;
  
          ViewBag.adminId = adminId;
      }
  
      IEnumerable&lt;blsEventStack&gt; returnedEventStacks = viewTypeRelatedEventStacks;
  
      if (orderColumn == "stack")
      {
          if (sortOrder == "descendingStack")
          {
              returnedEventStacks = from x in viewTypeRelatedEventStacks
                                    orderby x.EventStackId descending
                                    select x;
              sortOrder = null;
          }
          else
          {
              returnedEventStacks = from x in viewTypeRelatedEventStacks
                                    select x;
              sortOrder = "descendingStack";
          }
      }
  
      if (orderColumn == "managingAdmin")
      {
          if (sortOrder == "descendingManagingAdmin")
          {
              returnedEventStacks = from x in viewTypeRelatedEventStacks
                                    orderby x.ManagingAdminName descending
                                    select x;
              sortOrder = null;
          }
          else
          {
              returnedEventStacks = from x in viewTypeRelatedEventStacks
                                    orderby x.ManagingAdminName
                                    select x;
              sortOrder = "descendingManagingAdmin";
          }
      }
  
      if (orderColumn == "relatedClient")
      {
          if (sortOrder == "descendingRelatedClient")
          {
              returnedEventStacks = from x in viewTypeRelatedEventStacks
                                    orderby x.RelatedClientName descending
                                    select x;
              sortOrder = null;
          }
          else
          {
              returnedEventStacks = from x in viewTypeRelatedEventStacks
                                    orderby x.RelatedClientName
                                    select x;
              sortOrder = "descendingRelatedClient";
          }
      }
  
      if (orderColumn == "stackType")
      {
          if (sortOrder == "descendingStackType")
          {
              returnedEventStacks = from x in viewTypeRelatedEventStacks
                                    orderby x.EventStackType descending
                                    select x;
              sortOrder = null;
          }
          else
          {
              returnedEventStacks = from x in viewTypeRelatedEventStacks
                                    orderby x.EventStackType
                                    select x;
              sortOrder = "descendingStackType";
          }
      }
  
      if (orderColumn == "latestEventTime")
      {
          if (sortOrder == "descendingLatestEventTime")
          {
              returnedEventStacks = from x in viewTypeRelatedEventStacks
                                    orderby x.EventStackLastEventTime descending
                                    select x;
              sortOrder = null;
          }
          else
          {
              returnedEventStacks = from x in viewTypeRelatedEventStacks
                                    orderby x.EventStackLastEventTime
                                    select x;
              sortOrder = "descendingLatestEventTime";
          }
      }
  
      if (orderColumn == "latestEventStatus")
      {
          if (sortOrder == "descendingLatestEventStatus")
          {
              returnedEventStacks = from x in viewTypeRelatedEventStacks
                                    orderby x.EventStackLastEventStatus descending
                                    select x;
              sortOrder = null;
          }
          else
          {
              returnedEventStacks = from x in viewTypeRelatedEventStacks
                                    orderby x.EventStackLastEventStatus
                                    select x;
              sortOrder = "descendingLatestEventStatus";
          }
      }
  
      List&lt;ViewModelAllEventStacks&gt; returnedViewModel = new List&lt;ViewModelAllEventStacks&gt;();
  
      foreach (var item in returnedEventStacks)
      {
          returnedViewModel.Add( 
          new ViewModelAllEventStacks
          {
              EventStack = item,
              AdminId = item.AdminEventLogs.FirstOrDefault().AdminId,
              ClientId = item.ClientEventLogs.FirstOrDefault().ClientId
          });
      }
  
      ViewBag.stackNumber = stackNumber;
      ViewBag.adminName = adminName;
      ViewBag.clientName = clientName;
      ViewBag.stackType = stackType;
      ViewBag.stackStatus = stackStatus;
  
      // ViewBag passing state of existing order
      ViewBag.sortOrder = sortOrder;
  
      // ViewBag passing view data (ex. assignedEventStacks, allEventStacks, etc.)
      ViewBag.viewType = viewType;
  
      return PartialView("BLS_AllEventStacks", returnedViewModel);
  }
  </code></pre>