0

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

Leathel
  • 345
  • 4
  • 14
  • 1
    Apparently you a getting a count that's null on that line: `if (dictionaryList[i].TryGetValue(lineContentsBrand, out counts))` – gdir Jun 27 '17 at 15:34
  • 1
    It's not your initialization; it's `dictionaryList[i].TryGetValue(lineContentsBrand, out counts)`. Your `dictionaryList` doesn't give you a good array back (for whatever reason). – Kenneth K. Jun 27 '17 at 15:34
  • When using TryGetValue, if it evaluates to false, it will write NULL to the out variable. Try using Dictionary.ContainsKey and then assigning the value directly. – brandonstrong Jun 27 '17 at 15:39
  • I will give that a try, thanks for the help! – Leathel Jun 27 '17 at 15:40
  • Also note that when you are adding an entry to the dictionary you are adding a reference to the count array NOT adding a copy of the count array. This will mean that all entries in the dictionary refer to the same array and any changes made to one entry will affect all dictionary entries. – PaulF Jun 27 '17 at 15:43
  • You are correct, however that is the way it's supposed to work. =) – Leathel Jun 27 '17 at 18:18

0 Answers0