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?