-2

I have the following code:

public class Items
{
    public string ItemName { get; set; }
}
public class Orders
{
    public List<Items> ItemList { get; set; }
}

When I try to create an instance of the order object, the ItemList property initialization throws a null error saying that the get property of ItemList is null. The error is thrown at orders.ItemList.Add(itm) line. The answer might be obvious and simple, but I can't see it. Here is the code:

Orders orders = new Orders();
for (int i = 1; i < 10; i++)
{
    Items itm = new Items
    {
        ItemName = $"Item {i}"
    };
    orders.ItemList.Add(itm);
}

What silly mistake am I making here?

Xami Yen
  • 247
  • 1
  • 7
  • The mistake you're making is that you're not initializing the `ItemList` property, so it will be `null`, and you can't do `.Add(itm)` on a null reference. – mbj Jan 16 '19 at 21:54
  • 1
    there is no default value and no construct, thus its null –  Jan 16 '19 at 21:54
  • `orders.ItemList = new List();` – Rufus L Jan 16 '19 at 21:57
  • 1
    Generally speaking, class names should not be plural unless they represent a collection. So your classes should be called `Item` and `Order`, and the list property on `Order` should be `Items`. Also, it's usually not recommended to include the class name as part of a property name, so the `ItemName` property should just be `Name` (it's a little redundant to do `item.ItemName` instead of just `item.Name`) – Rufus L Jan 16 '19 at 21:58
  • @Rufus Thanks for the note. Appreciate that. – Xami Yen Jan 16 '19 at 21:59

4 Answers4

3

Your not initializing the property so it is null.

Try this:

public class Orders
{
    public List<Items> ItemList { get; set; } = new List<Items>();
}
Neil B
  • 1,901
  • 1
  • 8
  • 19
0

The property is set to whatever it's default is, in this case null. You probably want to set it in the constructor, and make the setter private so it can't be set to null later.

public class Orders
{
    public Orders()
    {
        ItemList = new List<Items>();
    }

    public List<Items> ItemList { get; private set; }
}
Grant Winney
  • 61,140
  • 9
  • 100
  • 152
0

You forgot to initialize the ItemsList, therefore you get a nullpointer exception

public class Orders
{
    public List<Items> ItemList { get; set; } = new List<Items>();
}
Bruno Caceiro
  • 6,179
  • 1
  • 19
  • 37
  • 1
    Perfect. That worked. What a silly mistake that I am making! – Xami Yen Jan 16 '19 at 21:59
  • It is normal, specially if you are a beginner. Try to understand the problem, and next time you won't fail again :) Don't forget to mark the answer as correct – Bruno Caceiro Jan 16 '19 at 22:01
0

List has not been initialized with a new instance of List.

orders.ItemList = new List<Items>();

Code will look like this:

Orders orders = new Orders();
orders.ItemList = new List<Items>();
for (int i = 1; i < 10; i++)
{
    Items itm = new Items
    {
        ItemName = $"Item {i}"
    };
    orders.ItemList.Add(itm);
}
user1579234
  • 481
  • 5
  • 15