NullReferenceException when accessing an array of Rectangle Shapes

39 views Asked by At

I'm struggling to understand why accessing an object in this array throws a NullReferenceException. I initialized the array right beforehand, so what's trying to be referenced should exist. parent.rowlength and parent.collength are the size of an array initialized earlier in code, and i know they return the right values. I'm pretty much out of ideas here. Edit: I should clarify, this happens on the first iteration of the for loops. System.NullReferenceException: 'Object reference not set to an instance of an object.'

Here's the code in the picture, let me know if you need to see more.

        RectangleShape[,] cells = new RectangleShape[parent.rowLength, parent.colLength];
        for (int i = 0; i < parent.rowLength; i++)
        {
            for (int j = 0; j < parent.colLength; j++)
            {
                Console.WriteLine(i + "   " + j);
                cells[i, j].Size = new Vector2f(10, 10);
                cells[i, j].Origin = new Vector2f(0, 0);
                cells[i, j].Position = new Vector2f(i * 10, j * 10);
                cells[i, j].FillColor = Color.White;
            }
        }
0

There are 0 answers