RemarkLima
RemarkLima

Reputation: 12047

"x.Groups = list" works but "x.Groups.Add(listItem)" throws Null Reference Exception?

I have a class, called Permissions, it has a few subclasses (and some properties), Group and Group.Permission.

Now, I CAN do the following

Permissions u = new Permissions();
u.userId = (Guid)user.ProviderUserKey;
List<int> groups = getGroupsForUserId(u.userId.ToString());

List<Permissions.Group> groupItems = new List<Permissions.Group>();

foreach (int g in groups)
{
    Permissions.Group groupItem = new Permissions.Group();
    groupItem.group_id = g;
    groupItem.Records = getRecordsForGroupId(g);
    groupItem.Permissions = getPermissionsForGroupId(g);

    groupItems.Add(groupItem);
}
u.Groups = groupItems;

However, I CAN'T do this:

Permissions u = new Permissions();
u.userId = (Guid)user.ProviderUserKey;
List<int> groups = getGroupsForUserId(u.userId.ToString());

foreach (int g in groups)
{
    Permissions.Group groupItem = new Permissions.Group();
    groupItem.group_id = g;
    groupItem.Records = getRecordsForGroupId(g);
    groupItem.Permissions = getPermissionsForGroupId(g);

    u.Groups.Add(groupItem);
}

My question, why?

I created a new Permissions.Group object, added the values and then added it to the u object's list of Groups. To my mind that should work, however I assume I've not wired something up in my class? My class just is a list of properties property_name { get; set; } so isn't anything exotic really.

Trying the latter, I get a NullReferenceException on the line u.Groups.Add(groupItem);

Upvotes: 1

Views: 107

Answers (4)

Tilak
Tilak

Reputation: 30728

You need to initialize Groups in the Permissions constructor.

Alternatively, If you do not want to modify the constructor, you can use object initializer.

Example:

Change Permissions u = new Permissions();

to

Permissions u = new Permissions(){Groups = new List<Permissions.Group>()};

In the object initializer, Groups is initialized to an empty List.

Upvotes: 1

SPFiredrake
SPFiredrake

Reputation: 3892

Because you haven't instantiated User.Groups yet, it still is null. Either in the constructor of User or after instantiating User you have to also instantiate the Groups property/field.

public class Permissions
{
    public List<Premissions.Group> Groups { get; set; }
    ...
    public Permissions()
    {
        Groups = new List<Permissions.Group>();
    }
}

OR

Permissions user = new Permissions();
user.Groups = new List<Permissions.Group>();

OR

public class Permissions
{
    private List<Permissions.Group> _groups = new List<Permissions.Group>();
    public List<Permissions.Group> Groups { get { return _groups; } set { _groups = value; } }
}

Upvotes: 1

Jeremy
Jeremy

Reputation: 9030

In the second example, my money is on the fact that you aren't initializing the Groups collection in Permissions object. So, in essence, you are attempting to add a group item to a null Groups collection.

Upvotes: 1

jrummell
jrummell

Reputation: 43097

You probably need to initialize Permissions.Groups in your Permissions constructor:

public class Permissions
{
    public Permissions()
    {
         Groups = new List<Group>();
    }

    public ICollection<Group> Groups { get; set; }
}

Upvotes: 4

Related Questions