Heidel
Heidel

Reputation: 3254

Exception NullReferenceException in controller (asp.net mvc)

There are Supplier model and User model in my project, every Supplier has a few Users
Supplier model

public class SupplierRow
{
    public Guid Id { get; set; }
    public string FullName { get; set; }
    public bool Subscribed { get; set; }
    public bool Active { get; set; }
    public int Visits { get; set; }

    public List<UserRow> Users { get; set; }

    public bool AllInactive
    {
        get
        {
            foreach (UserRow ur in Users)
            {
                if (ur.Status == 1) return false;
            }
            return true;
        }
    }
}

and User model

public class UserRow
{
    public Guid Id { get; set; }
    public string FullName { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
    public int Status { get; set; }
    public int Role { get; set; }

    public Guid SupplierId { get; set; }
}

then I use my models in controller

 public ActionResult Grid(bool? active)
    {
        var suppliers = Context.Suppliers.AsNoTracking()
            .WhereIf(active != null, e => e.Active == active)
            .Select(e => new SupplierRow
                            {
                                Id = e.Id,
                                FullName = e.FullName,
                                Active = e.Active,
                                Visits = e.Visits,
                            })
                            .ToList();


        List<Guid> supplierIds = new List<Guid>();
        foreach (SupplierRow sr in suppliers)
        {
            supplierIds.Add(sr.Id);
        }

        var users = Context.Users.AsNoTracking()
            .Where(e => supplierIds.Contains(e.SupplierId.Value))
            .Select(e => new UserRow
            {
                Id = e.Id,
                FullName = e.FullName,
                Email = e.Email,
                Name = e.Name,
                Status = e.Status,
                Role = e.Role,
                SupplierId = e.SupplierId.Value
            }).ToList();


        foreach (UserRow ur in users) 
        {
            foreach (SupplierRow sr in suppliers) 
            {
                if (ur.SupplierId == sr.Id) 
                {
                    sr.Users.Add(ur);
                } 
            }
        }

        return PartialView("_Grid", suppliers);
    }

but when I try to debug my project I get some exception here enter image description here What's wrong? How can I fix that?

Upvotes: 0

Views: 90

Answers (1)

Zabavsky
Zabavsky

Reputation: 13640

Your Users list are not initialized. Create a new list before accessing it Users = new List<UserRow>(); You can change the SupplierRow class:

public class SupplierRow {
    private List<UserRow> users = new List<UserRow>();

    public List<UserRow> Users
    {
        get { return users; }
        set { users = value; }
    }
    ...
}

or in the constructor:

public class SupplierRow
{
    public SupplierRow()
    {
        Users = new List<UserRow>();
    }
    public List<UserRow> Users { get; set; }
    ...
}

or before accessing it:

foreach (UserRow ur in users) 
{
    foreach (SupplierRow sr in suppliers) 
    {
        sr.Users = new List<UserRow>();
        if (ur.SupplierId == sr.Id) 
        {
            sr.Users.Add(ur);
        } 
    }
}

or you can just use linq:

foreach (SupplierRow sr in suppliers) 
{
    sr.Users = users.Where(user => user.SupplierId == sr.Id);
}

return PartialView("_Grid", suppliers);

Upvotes: 2

Related Questions