title: "Object not set to an instance of an object" using code that works in one instance, does not work in another instance
body: <p>I am using VS'15 on asp.net 4.6.</p>
  
  <p>I am also modeling my application after the N-Tier design found at:</p>
  
  <p><a href="http://blog.magnusmontin.net/2013/05/30/generic-dal-using-entity-framework/" rel="nofollow">Implementing a generic data access layer using Entity Framework</a></p>
  
  <p>I am able to return data from a User table, however when using code based on the User code to return data from another table, I get a null return.</p>
  
  <p>My BLL code that works:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using Library.DataAccessLayer;
  using Library.Model;
  
  namespace Library.BusinessLogicLayer
  {
      public interface IBusinessLogicLayer_User
      {
          IList&lt;UsersModel&gt; GetAllUsers();
          UsersModel GetUserByAapNetUserID(string _UserID);
          void UpdateUser(params UsersModel[] _UserModel);
      }
  
      public class BusinessLogicLayer_User : IBusinessLogicLayer_User
      {
          private readonly IUsersRepository _UsersRepository;
  
          public BusinessLogicLayer_User()
          {
              _UsersRepository = new UsersRepository();
          }
  
          public BusinessLogicLayer_User(IUsersRepository userRepository)
          {
              _UsersRepository = userRepository;
          }
  
          public IList&lt;UsersModel&gt; GetAllUsers()
          {
              return _UsersRepository.GetAll();
          }
  
          public UsersModel GetUserByAapNetUserID(string _UserID)
          {
              return _UsersRepository.GetSingle(u =&gt; u.AspNetUserID.Equals(_UserID));
          }
  
          public void UpdateUser(params UsersModel[] _UsersModel)
          {
              _UsersRepository.Update(_UsersModel);
          }
      }
  }
  </code></pre>
  
  <p>My BLL code that does not work:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using Library.DataAccessLayer;
  using Library.Model;
  
  namespace Library.BusinessLogicLayer
  {
      public interface IBusinessLogicLayer_AreaSecurity
      {
          AreaSecurityModel GetAreaSecurityByUserID(string _AspNetUserID);
          void AddAreaSecurity(params AreaSecurityModel[] _AreaSecurityModel);
          void UpdateAreaSecurity(params AreaSecurityModel[] _AreaSecurityModel);
  
          IList&lt;AreaSecurityModel&gt; GetAllAreaSecurity();
      }
  
      public class BusinessLogicLayer_AreaSecurity : IBusinessLogicLayer_AreaSecurity
      {
          private readonly IAreaSecurityRepository _AreaSecurityRepository;
  
          public BusinessLogicLayer_AreaSecurity()
          {
              _AreaSecurityRepository = new AreaSecurityRepository();
          }
  
          public BusinessLogicLayer_AreaSecurity(IAreaSecurityRepository areaSecurityRepository)
          {
              _AreaSecurityRepository = areaSecurityRepository;
          }
  
          public void AddAreaSecurity(params AreaSecurityModel[] _AreaSecurityModel)
          {
              _AreaSecurityRepository.Add(_AreaSecurityModel);
          }
  
          public IList&lt;AreaSecurityModel&gt; GetAllAreaSecurity()
          {
              return _AreaSecurityRepository.GetAll();
          }
  
          public AreaSecurityModel GetAreaSecurityByUserID(string _AspNetUserID)
          {
              return _AreaSecurityRepository.GetSingle(r =&gt; r.AspNetUserID.Equals(_AspNetUserID));
          }
  
          public void UpdateAreaSecurity(params AreaSecurityModel[] _AreaSecurityModel)
          {
              _AreaSecurityRepository.Update(_AreaSecurityModel);
          }
      }
  }
  </code></pre>
  
  <p>And here is the code that is calling the stuff that works and does not work:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.Mvc;
  using Library.BusinessLogicLayer;
  using Library.Model;
  
  
  namespace IdahoFalls9thWardBulletin.Controllers
  {
      public class ApplicationBaseController : Controller
      {
          protected override void OnActionExecuted(ActionExecutedContext filterContext)
          {
              if (User != null)
              {
                  var context = new ApplicationDbContext();
                  var username = User.Identity.Name;
  
                  if (!string.IsNullOrEmpty(username))
                  {
                      var user = context.Users.SingleOrDefault(u =&gt; u.UserName == username);
  
                      BusinessLogicLayer_User _BusinessLogicLayer_User = new BusinessLogicLayer_User();
                      var UserDetails = _BusinessLogicLayer_User.GetUserByAapNetUserID(user.Id);
                      ViewData.Add("FirstName", UserDetails.FirstName);
  
                      BusinessLogicLayer_AreaSecurity _BusinessLogicLayer_AreaSecurity = new BusinessLogicLayer_AreaSecurity();
                      AreaSecurityModel _AreaSecurityModel = _BusinessLogicLayer_AreaSecurity
                          .GetAllAreaSecurity()
                          .ToList()
                          .FirstOrDefault();
                      ViewData.Add("AreaSecurity", _AreaSecurityModel);
                  }
              }
              base.OnActionExecuted(filterContext);
          }
          public ApplicationBaseController()
          {
          }
      }
  }
  </code></pre>
  
  <p>What I do not understand is that when I do <code>_BusinessLogicLayer_AreaSecurity.GetAllAreaSecurity().ToList().FirstOrDefault()</code> It does in fact return the list of all records.</p>
  
  <p>However when I do <code>_BusinessLogicLayer_AreaSecurity.GetAllAreaSecurity().GetAreaSecurityByUserID(user.Id)</code> It returns the null value.</p>
  
  <p>I have no idea what I am missing here. This is basically my first attempt at MVC code first.</p>