I am constantly getting a NullReferenceException when attempting to increment a value in an int[]. I have tried initializing the array (counts) multiple ways, such as
int[] counts = {0, 0};
int[] counts = new int[]{0, 0};
int[] counts = new int[2]{0, 0};
int[] counts = new int[2];
None of these ways gives me a different result, see code below:
int[] counts = new int[2] {0, 0};
Console.WriteLine("Incrementing the warehouse dictionary at index: " + i);
//This will check and see if it's contained
if (warehouseCostsArray[i] != "")
{
//This will check and see if it has the lowest price
if (warehouseCostsArray[i] == "0")
{
//This will check and see if this brand already exists in the warehouse dictionary, it will return the counts if it is found
if (dictionaryList[i].TryGetValue(lineContentsBrand, out counts))
{
Console.WriteLine("11\n");
//incrememnt the counts based on the conditions
counts[0]++;
counts[1]++;
//reassign that value in the dictionary
dictionaryList[i][lineContentsBrand] = counts;
}
//same thing as above except this is triggered if that brand does not exist
else
{
Console.WriteLine("22\n");
counts[0] = 1;
counts[1] = 1;
dictionaryList[i].Add(lineContentsBrand, counts);
}
}
//This is triggered if the warehouse contains the sku but does not have the lowest price.
else
{
//same thing as above except this time we are not going increment counts[1]
if (dictionaryList[i].TryGetValue(lineContentsBrand, out counts))
{
Console.WriteLine("33\n");
counts[0]++;
dictionaryList[i][lineContentsBrand] = counts;
}
else
{
Console.WriteLine("44\n");
//This will add if it doesnt exist, leaving counts[1] as 0
Console.WriteLine("Incrementing counts 0");
counts[0] = 1;
Console.WriteLine("Incrementing counts 1");
counts[1] = 0;
Console.WriteLine("Adding entry to the dictionary");
dictionaryList[i].Add(lineContentsBrand, counts);
}
}
}
The error is happening in the last else when trying to set
counts[0]=1;
Here is an included pic of the output Console output
I know this is a question that is asked a lot, but I am totally stuck and cant seem to get past this. Any help is appreciated!
Thanks, Ben