C
C#3mo ago
js

object reference not set to an instance of object error

int selection = ConsoleHelpers.GetIntegerInRange(1, count10 - 1, "Please select the item to add to order.");

//var pizzas2 = FoodMenu.pizzas.ToArray();

//var burgers2 = FoodMenu.burgers.ToArray();

int count = 0;

foreach (var pizza2 in FoodMenu.pizzas)
{
selectionItems[count] = pizza2.ToString();
count++;
}
int selection = ConsoleHelpers.GetIntegerInRange(1, count10 - 1, "Please select the item to add to order.");

//var pizzas2 = FoodMenu.pizzas.ToArray();

//var burgers2 = FoodMenu.burgers.ToArray();

int count = 0;

foreach (var pizza2 in FoodMenu.pizzas)
{
selectionItems[count] = pizza2.ToString();
count++;
}
No description
2 Replies
js
js3mo ago
selectionItems is defined like this at the start of my class
internal string[] selectionItems;
internal string[] selectionItems;
i dont understand whats wrong
Jimmacle
Jimmacle3mo ago
the default value of reference types is null in addition, arrays have a fixed size so you can't just assign a value to any index you want you probably want to use a list, it will get bigger as you add more things to it
internal List<string> selectionItems = [];

...

selectionItems.Add(pizza2.ToString());
internal List<string> selectionItems = [];

...

selectionItems.Add(pizza2.ToString());