473,508 Members | 2,327 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What wrong with this code ?

Hi Experts:

The program creates Panels and inicilize them. When I run the code below,
the program crashes (at the line commented below). Any help is appriciated.

Thanks in Advance!
Polaris
private void createPanels (int nPanelCount)
{
Panel [] parray = new Panel [nPanelCount];

for (int i = 0; i < nPanelCount; i++ )
{
parray[i].Parent = this; // dead here becaue parray[i] is null
parray[i].Dock = DockStyle.Left;
}
}
May 29 '06 #1
8 1431
you need
parray[i] = new Panel();

May 29 '06 #2
Strange, code seems to be correct.
Maybe something is wrong with Parent property?
OR
parray is class variable
I mean smth like this
public class C
{
T var;

public void method()
{
T var = new T();

var.DoSomething();
}
}
--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com

"Polaris" <et*******@hotmail.com> wrote in message
news:Ol**************@TK2MSFTNGP03.phx.gbl...
Hi Experts:

The program creates Panels and inicilize them. When I run the code below,
the program crashes (at the line commented below). Any help is
appriciated.

Thanks in Advance!
Polaris
private void createPanels (int nPanelCount)
{
Panel [] parray = new Panel [nPanelCount];

for (int i = 0; i < nPanelCount; i++ )
{
parray[i].Parent = this; // dead here becaue parray[i] is null
parray[i].Dock = DockStyle.Left;
}
}

May 29 '06 #3
Polaris <et*******@hotmail.com> wrote:
The program creates Panels and inicilize them. When I run the code below,
the program crashes (at the line commented below). Any help is appriciated.

private void createPanels (int nPanelCount)
{
Panel [] parray = new Panel [nPanelCount];

for (int i = 0; i < nPanelCount; i++ )
{
parray[i].Parent = this; // dead here becaue parray[i] is null
parray[i].Dock = DockStyle.Left;
}
}


You need:

parray[i] = new Panel();

in the loop. Your comment is exactly right - parray[i] is null, because
you haven't told it to be anything else.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 29 '06 #4
Thanks but shouldn't the code below create 3 instances of Panel?
Panel[] parray = new Panel [3];

"Polaris" <et*******@hotmail.com> wrote in message
news:Ol**************@TK2MSFTNGP03.phx.gbl...
Hi Experts:

The program creates Panels and inicilize them. When I run the code below,
the program crashes (at the line commented below). Any help is
appriciated.

Thanks in Advance!
Polaris
private void createPanels (int nPanelCount)
{
Panel [] parray = new Panel [nPanelCount];

for (int i = 0; i < nPanelCount; i++ )
{
parray[i].Parent = this; // dead here becaue parray[i] is null
parray[i].Dock = DockStyle.Left;
}
}

May 29 '06 #5
No, it creates an array of size 3 that can handle objects of type
panel.
You still need to instantiate the objects.

This is also obviously evident in the error message you were getting,
indicating that your object was null.

Perhaps this would work (untested):
Panel[] parray = new Panel [3] {new Panel(), new Panel(), new Panel()};

May 29 '06 #6
"Polaris" <et*******@hotmail.com> wrote in message
news:O7**************@TK2MSFTNGP02.phx.gbl...
Thanks but shouldn't the code below create 3 instances of Panel?
Panel[] parray = new Panel [3];


Nope

It creates an array of 3 panel References each set to null
You need to explicitly create the individual panel objects

Bill
May 29 '06 #7
Hi there,

I think the problem is that you are initializing an array of panels,
but this doesn't create the panels themselves, just the array to hold
them. Each element in the array should first have something like
parray[1]=new Panel(); then you can assign it's parent, etc.
Polaris wrote:
Hi Experts:

The program creates Panels and inicilize them. When I run the code below,
the program crashes (at the line commented below). Any help is appriciated.

Thanks in Advance!
Polaris
private void createPanels (int nPanelCount)
{
Panel [] parray = new Panel [nPanelCount];

for (int i = 0; i < nPanelCount; i++ )
{
parray[i].Parent = this; // dead here becaue parray[i] is null
parray[i].Dock = DockStyle.Left;
}
}


May 30 '06 #8
Thanks everyone !

"Polaris" <et*******@hotmail.com> wrote in message
news:Ol**************@TK2MSFTNGP03.phx.gbl...
Hi Experts:

The program creates Panels and inicilize them. When I run the code below,
the program crashes (at the line commented below). Any help is
appriciated.

Thanks in Advance!
Polaris
private void createPanels (int nPanelCount)
{
Panel [] parray = new Panel [nPanelCount];

for (int i = 0; i < nPanelCount; i++ )
{
parray[i].Parent = this; // dead here becaue parray[i] is null
parray[i].Dock = DockStyle.Left;
}
}

May 30 '06 #9

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

Similar topics

125
14541
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
72
5737
by: E. Robert Tisdale | last post by:
What makes a good C/C++ programmer? Would you be surprised if I told you that it has almost nothing to do with your knowledge of C or C++? There isn't much difference in productivity, for...
121
9907
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode...
51
13297
by: WindAndWaves | last post by:
Can anyone tell me what is wrong with the goto command. I noticed it is one of those NEVER USE. I can understand that it may lead to confusing code, but I often use it like this: is this...
46
4154
by: Keith K | last post by:
Having developed with VB since 1992, I am now VERY interested in C#. I've written several applications with C# and I do enjoy the language. What C# Needs: There are a few things that I do...
13
4999
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
1
1457
by: GS | last post by:
I got a combobox box that I load at load time. the Item and vales ended up in reverse order of each other, what went wrong? the database table has the following row code value ebay ...
98
4490
by: tjb | last post by:
I often see code like this: /// <summary> /// Removes a node. /// </summary> /// <param name="node">The node to remove.</param> public void RemoveNode(Node node) { <...> }
9
2110
by: Pyenos | last post by:
import cPickle, shelve could someone tell me what things are wrong with my code? class progress: PROGRESS_TABLE_ACTIONS= DEFAULT_PROGRESS_DATA_FILE="progress_data" PROGRESS_OUTCOMES=
20
2795
by: Daniel.C | last post by:
Hello. I just copied this code from my book with no modification : #include <stdio.h> /* count characters in input; 1st version */ main() { long nc; nc = 0;
0
7120
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...
0
7323
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,...
1
7039
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...
0
5626
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 project—planning, coding, testing,...
0
4706
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3192
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1553
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 ...
1
763
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
415
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...

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.