what's the problem with this array initalization?

J

John Salerno

Here's my method, and I get an error on the array initialization line.
I've gotte this same error before, and it was fixed by adding the first
line of the method, so I'm not sure what to do now.


private static Status[][][,] ConvertToFlags(int[][][,] allPanels)
{
Status[][][,] panel = new Status[4][][,];

for (int i = 0; i < 4; i++)
for (int j = 0; j < 8; j++)
{
panel[j] = new Status[2, 10]; //Object
reference not set to an instance of an object.

for (int x = 0; x < 2; x++)
for (int y = 0; y < 10; y++)
{
if (allPanels[j][x, y] == 0)
panel[j][x, y] = Status.Off;
else
panel[j][x, y] = Status.Red;
}
}

return panel;
}
 
L

Lloyd Dupont

uh, and you expect that to work?
Status[][][,] panel = new Status[4][][,];

for (int i = 0; i < 4; i++)
for (int j = 0; j < 8; j++)
{
panel[j] = new Status[2, 10]; //Object


panel exists. it's null.
therefore panel[j] will throw a NullReferenceExcpetion

I don't know how you should fix that in your business logic, but at first
glance I will do that
for (int i = 0; i < 4; i++)
{
panel = new Status[8][,];
for (int j = 0; j < 8; j++)
{
panel[j] = new Status[2, 10]; //Object



John Salerno said:
Here's my method, and I get an error on the array initialization line.
I've gotte this same error before, and it was fixed by adding the first
line of the method, so I'm not sure what to do now.


private static Status[][][,] ConvertToFlags(int[][][,] allPanels)
{
Status[][][,] panel = new Status[4][][,];

for (int i = 0; i < 4; i++)
for (int j = 0; j < 8; j++)
{
panel[j] = new Status[2, 10]; //Object reference
not set to an instance of an object.

for (int x = 0; x < 2; x++)
for (int y = 0; y < 10; y++)
{
if (allPanels[j][x, y] == 0)
panel[j][x, y] = Status.Off;
else
panel[j][x, y] = Status.Red;
}
}

return panel;
}
 
J

John Salerno

John said:
Here's my method, and I get an error on the array initialization line.
I've gotte this same error before, and it was fixed by adding the first
line of the method, so I'm not sure what to do now.


private static Status[][][,] ConvertToFlags(int[][][,] allPanels)
{
Status[][][,] panel = new Status[4][][,];

for (int i = 0; i < 4; i++)
for (int j = 0; j < 8; j++)
{
panel[j] = new Status[2, 10]; //Object
reference not set to an instance of an object.

for (int x = 0; x < 2; x++)
for (int y = 0; y < 10; y++)
{
if (allPanels[j][x, y] == 0)
panel[j][x, y] = Status.Off;
else
panel[j][x, y] = Status.Red;
}
}

return panel;
}


This seems to have done the trick:

panel = new Status[8][,];
panel[j] = new Status[2, 10];


Boy, arrays can be confusing! :)
 
J

John Salerno

John said:
This seems to have done the trick:

panel = new Status[8][,];
panel[j] = new Status[2, 10];


Boy, arrays can be confusing! :)


Maybe I spoke too soon. The above works, but after I added these lines
to test it:

Console.WriteLine(flaggedPanels[0][1][1, 1]);
Console.ReadLine();

I get that same reference not found exception on the WriteLine line. Why
would the code work without the above lines, but not with them?
 
J

John Salerno

John said:
John Salerno wrote:

This seems to have done the trick:

panel = new Status[8][,];
panel[j] = new Status[2, 10];


Boy, arrays can be confusing! :)



Maybe I spoke too soon. The above works, but after I added these lines
to test it:

Console.WriteLine(flaggedPanels[0][1][1, 1]);
Console.ReadLine();

I get that same reference not found exception on the WriteLine line. Why
would the code work without the above lines, but not with them?


Forgive me once again. I moved the line

panel = new Status[8][,];

into the first for loop instead of where it was, and that fixed it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top