473,659 Members | 2,526 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

what's the problem with this array initalization?

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[i][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[i][j][x, y] == 0)
panel[i][j][x, y] = Status.Off;
else
panel[i][j][x, y] = Status.Red;
}
}

return panel;
}
Nov 20 '05 #1
4 1381
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[i][j] = new Status[2, 10]; //Object
panel[i] exists. it's null.
therefore panel[i][j] will throw a NullReferenceEx cpetion

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[i] = new Status[8][,]; for (int j = 0; j < 8; j++)
{
panel[i][j] = new Status[2, 10]; //Object

"John Salerno" <jo******@NOSPA Mgmail.com> wrote in message
news:rr******** *************** *******@rcn.net ... 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[i][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[i][j][x, y] == 0)
panel[i][j][x, y] = Status.Off;
else
panel[i][j][x, y] = Status.Red;
}
}

return panel;
}

Nov 20 '05 #2
John Salerno wrote:
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[i][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[i][j][x, y] == 0)
panel[i][j][x, y] = Status.Off;
else
panel[i][j][x, y] = Status.Red;
}
}

return panel;
}


This seems to have done the trick:

panel[i] = new Status[8][,];
panel[i][j] = new Status[2, 10];
Boy, arrays can be confusing! :)
Nov 20 '05 #3
John Salerno wrote:
This seems to have done the trick:

panel[i] = new Status[8][,];
panel[i][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.WriteLi ne(flaggedPanel s[0][1][1, 1]);
Console.ReadLin e();

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?
Nov 20 '05 #4
John Salerno wrote:
John Salerno wrote:

This seems to have done the trick:

panel[i] = new Status[8][,];
panel[i][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.WriteLi ne(flaggedPanel s[0][1][1, 1]);
Console.ReadLin e();

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[i] = new Status[8][,];

into the first for loop instead of where it was, and that fixed it.
Nov 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
2304
by: K.M. Jr. | last post by:
Hi all, Consider this line - char s = "\0"; Does this initialize all array elements to zero ? Thanks.
13
5035
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
6
5623
by: easy | last post by:
The following code gives me compiler errors galore telling me that I'm crossing the initalization of the vectors and that case label 2 and default are "within scope of cleanup or variable array". I suspect its the destructors for the vectors that is causing the problem but why are they being called at all? Shouldn't they go out of scope at the closing bracket after default? Putting brackets around the contents of case1 solves my...
1
1463
by: Yves | last post by:
Hi! I was fiddling around a bit with iterators and I came across some strange behaviour. I have the following simple code: -- class A(object): def __init__(self, n): self.n = n
669
25860
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic paper written on this subject. On the Expressive Power of Programming Languages, by Matthias Felleisen, 1990. http://www.ccs.neu.edu/home/cobbe/pl-seminar-jr/notes/2003-sep-26/expressive-slides.pdf
7
2157
by: pallav | last post by:
I'm having some trouble with my copy constructor. I've tried using gdb to find the bug, but it seg faults in the destructor. I'm not able to see what I'm doing wrong. Since I'm using pointers, I need deep copy and I believe I'm doing that in my constructors. Can someone help me see what I'm missing out? Here is my code. typedef boost::shared_ptr<FactorFactorPtr; enum FactorTypeT
8
2061
by: watkinsdev | last post by:
Hi, I have created a mesh class in visual studio 6.0 c++. I can create a device, render objects and can edit the objects by for instancnce selecting a cluster of vertices and processing the vertices and can do this multiple times on a sinlge vertex cluster. The problem I have been encoutering is that, if I select a second vertex cluster and try to edit that , the program crashes.
11
3343
by: venkatagmail | last post by:
I have problem understanding pass by value and pass by reference and want to how how they are or appear in the memory: I had to get my basics right again. I create an array and try all possible ways of passing an array. In the following code, fun1(int a1) - same as fun1(int* a1) - where both are of the type passed by reference. Inside this function, another pointer a1 is created whose address &a1 is different from that of the passed...
3
3828
by: =?Utf-8?B?Y21lZWsxXzE5OTk=?= | last post by:
Hello, On a webpage, create an UpdatePanel with two DropDownLists. Set AutoPostBack of DropDownList1 to true. In the SelectedIndexChanged method, refill DropDownList2 and set the focus to DropDownList1 using the ScriptManager SetFocus method. Use the Visual Studio debugger. Set a breakpoint on in the (!IsPostBack) part of the PageLoad method. Start the web application and continue after the breakpoint has been reached.
0
8341
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8851
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8539
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8630
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7360
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4342
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2759
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1982
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1739
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.