I am creating a webapi for a shop type service. I am running into a NullReferenceException when using a class I have written which doesnt make sense. Here is my api code:
[HttpGet]
public HttpStatusCode Get()
{
Item item1 = new Item("Chips", 1, false);
Console.WriteLine(item1.ToString());
Items collection = new Items("a");
collection.addItem(item1);
Items collections = new Items("a");
collections.addItem(item1);
Console.WriteLine(collections.getSize());
return HttpStatusCode.OK;
}
My Item code:
public class Item
{
public Item(string Name, double Price, bool GlutenFree)
{
this.itemID = Guid.NewGuid();
this.name = Name;
this.price = Price;
this.glutenFree = GlutenFree;
}
public Guid itemID;
public string name;
public double price;
public bool glutenFree;
And lastly the items class:
public Items(string a){
this.name = a;
}
public string name;
public ArrayList itemCollection;
public int getSize()
{
The idea is an individual item will be part of a larger item collection - so for example a coke can will be part of a drinks collection.
The problem I am having is when I run this code it runs into an error at the adding an item to the items arraylist. I tried instantiating a local arraylist in the Get()
method and this does work, however it does not give me the flexibility I need and is not suitable. I am not sure why this error is happening, I have run a if (collection is null)
check and it does not return null for it. I am not sure why this error is happening and would appreciate advice
EDIT: Realised I am not doing new ArrayList() in my items class which meant a array list was never created. Fixed now.