do you have ContactInformation for the user ? My guess is user.ContactInformation is null and you are getting error when getting user.ContactInformation.Email.
Repository Pattern Null Reference Exception in Controller
-
04-10-2022 - |
Question
I have Implemented a Generic Repository Pattern and UnitOfWork but I get a Null Reference Exception in my Controller when I try to populate a new instance of a Model Class.
Here is my Repository Pattern:
class GenericRepository<TEntity> where TEntity : class
{
internal PropmetEntities context;
internal DbSet<TEntity> dbSet;
public GenericRepository(PropmetEntities context)
{
this.context = context;
this.dbSet = context.Set<TEntity>();
}
public virtual IEnumerable<TEntity> Get()
{
IQueryable<TEntity> query = dbSet;
return query.ToList();
}
public virtual TEntity GetByID(object id)
{
return dbSet.Find(id);
}
public virtual void Insert(TEntity entity)
{
dbSet.Add(entity);
}
public virtual void Delete(object id)
{
TEntity entityToDelete = dbSet.Find(id);
Delete(entityToDelete);
}
public virtual void Delete(TEntity entityToDelete)
{
if (context.Entry(entityToDelete).State == EntityState.Detached)
{
dbSet.Attach(entityToDelete);
}
dbSet.Remove(entityToDelete);
}
public virtual void Update(TEntity entityToUpdate)
{
dbSet.Attach(entityToUpdate);
context.Entry(entityToUpdate).State = EntityState.Modified;
}
}
Here is my unit of work:
class UnitOfWork
{
private PropmetEntities context = new PropmetEntities();
private GenericRepository<User> userRepository;
public GenericRepository<User> UserRepository
{
get
{
if (this.userRepository == null)
this.userRepository = new GenericRepository<User>(context);
return userRepository;
}
}
}
This is my Model Class:
public class UserGridModel
{
public int ID { get; set; }
public string Username {get; set;}
public string EMail { get; set; }
public string Surname { get; set; }
public string Role { get; set; }
public ContactInformation ContactInformation { get; set; }
public Role Role1 { get; set; }
}
And this is my controller action:
private UnitOfWork.UnitOfWork unitOfWork = new UnitOfWork.UnitOfWork();
[PrivilegeFilter(priv = Privileges.UserView)]
public ActionResult Grid(GridSettings set)
{
var userList = from user in unitOfWork.UserRepository.Get() select user;
var users = new List<UserGridModel>();
if (userList.Any())
{
foreach (var user in userList)
{
users.Add(new UserGridModel()
{
ID = user.ID,
Username = user.Username,
EMail = user.ContactInformation.EMail,
Surname = user.ContactInformation.Surname,
Role = user.Role1.Description
});
}
}
GridModel grid = new GridModel(set, users.Count());
grid.rows = users.Skip((set.PageIndex - 1) * set.PageSize).Take(set.PageSize).ToList();
return Json(grid, JsonRequestBehavior.AllowGet);
}
I get an Null Reference Exception here:
users.Add(new UserGridModel()
{
ID = user.ID,
Username = user.Username,
EMail = user.ContactInformation.EMail,
Surname = user.ContactInformation.Surname,
Role = user.Role1.Description
});
Solution
OTHER TIPS
Possible reasons are
user.ContactInformation == null
or user.Role1 == null
. You should add some check for nulls in your UserGridModel
creation code.
You could null
check by doing something like this:
foreach (var user in userList.Where(x => x.ContactInformation != null && x.Role1 != null))
{
users.Add(new UserGridModel()
{
ID = user.ID,
Username = user.Username,
EMail = user.ContactInformation.EMail,
Surname = user.ContactInformation.Surname,
Role = user.Role1.Description
});
}
This will at least prevent you from getting the Null Reference Exception when one of those properties is null
.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow