"Object reference not set to an instance of an object" error when trying to add objects to a list inside a class [duplicate]

StackOverflow https://stackoverflow.com/questions/23416458

Question

I have some custom classes defined that include lists of other classes like so:

public class Unit
{
    public string Name { get; set; }
    public List<Group> Contains { get; set; }
}

public class Group
{
    public string Name { get; set; }
    public Type Type { get; set; }
    public int Number { get; set; }
}

public static Type basic_1 = new Type() { Name = "basic_1", Number = 1, Cost = 13 };

The basic idea is that you have a unit which contains a list of groups, each with a different type. The type contains specific properties while the classes that contain them are organizational in nature.

I then try to start building out these classes like so:

Unit unit1 = new Unit() { Name = "Unit 1" };
Group group1 = new Group() { Name = "Group 1", Number = 10, Type = basic_1 };
unit1.Contains.Add(group1);

But here I receive the error (on the Add method) "Object reference not set to an instance of an object." Looking at the locals in the debugger I can see that the Unit, Group and Type were all created successfully and the group1 contains all the Type values it's supposed to, but unit1.Contains is null.

What am I doing wrong? FYI I've never done something like this before so I don't even know if it's possible, but it seems to work fine up until this point.

Was it helpful?

Solution

Your List<Group> Contains never gets initialized, so when you try to access it, you get a null reference exception. Essentially all null reference exceptions are the same, you're trying to use an object that's null.

In this case, let's just add a default constructor to initialize the list for us.

public class Unit
{
    public string Name { get; set; }

    public List<Group> Contains { get; set; }

    public Unit()
    {
        Contains = new List<Group>();
    }
}

By the way, Contains is a terrible name for a list. Contains is usually a function call, as it's a verb. Usually better to use a noun for a collection, such as Groups.

public class Unit
{
    public string Name { get; set; }

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

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

OTHER TIPS

Your Contains list within User class never gets instantiated.

You may want to change your User class to this instead:

public class Unit
{
    public string Name { get; set; }

    List<Group> list = new List<Group>();

    public List<Group> Contains
    {
        get { return list; }
    }
}

As you can see the Contains property now only returns a list instance that can of course be modified but never reaffected. Likely, this is what you'll want.

Although this is a tad outside the scope of your question, I would suggest that you rename the Contains property to Groups instead.

You have initialized the Unit But Unit.Contains Does not initialize. Before Adding objects to Contains list it must be initialized.

Unit unit1 = new Unit() { Name = "Unit 1" };
Group group1 = new Group() { Name = "Group 1", Number = 10, Type = basic_1 };
unit1.Contains = new List<Group>();
unit1.Contains.Add(group1);

Or

In your Unit class constructor you can initialize List here the code

public class Unit
{
public string Name { get; set; }
public List<Group> Contains { get; set; }
public Unit()
{
Contains = new List<Group>();
}
}

I would prefer the 2nd solution as it will make sure that whenever Unit is initialized Contains will be initialized automatically.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top