473,387 Members | 1,486 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

object ref not set ... resizing panel...

I was writing a C# program this week. The program works okay in my pc (in
both debug and release builds) but realized that it would not work to other
pcs. It simply would close after displaying an "This program has encountered
an error etc etc etc.." popup.

I added some try, catch blocks and found that the error message was: "Object
reference not set to an instance bla bla". After spending some time to
locate the line that causes the problem, I again realized that the code was
generated by the designer! And .. that I have no idea at all of what is
really happening. The lines that cause the problem are:

this.Resize += new System.EventHandler(this.frmGraphics_Resize);
this.ResumeLayout(false);

frmGraphics function is:

private void frmGraphics_Resize(object sender, EventArgs e) {
UpdateControls();
}

UpdateControls is self explanatory. It checks if text in various controls of
the form should be updated.

Can anyone tell me why this code generates this error?

Note that if I change the lines

this.Resize += new System.EventHandler(this.frmGraphics_Resize);
this.ResumeLayout(false);

to

this.ResumeLayout(false);
this.Resize += new System.EventHandler(this.frmGraphics_Resize);

Then it will work okay.

So ... what is the catch??? :/ Does anyone have any idea?

-nick

Dec 23 '06 #1
3 3641

Nick Valeontis wrote:
I was writing a C# program this week. The program works okay in my pc (in
both debug and release builds) but realized that it would not work to other
pcs. It simply would close after displaying an "This program has encountered
an error etc etc etc.." popup.

I added some try, catch blocks and found that the error message was: "Object
reference not set to an instance bla bla". After spending some time to
locate the line that causes the problem, I again realized that the code was
generated by the designer! And .. that I have no idea at all of what is
really happening. The lines that cause the problem are:

this.Resize += new System.EventHandler(this.frmGraphics_Resize);
this.ResumeLayout(false);

frmGraphics function is:

private void frmGraphics_Resize(object sender, EventArgs e) {
UpdateControls();
}

UpdateControls is self explanatory. It checks if text in various controls of
the form should be updated.

Can anyone tell me why this code generates this error?

Note that if I change the lines

this.Resize += new System.EventHandler(this.frmGraphics_Resize);
this.ResumeLayout(false);

to

this.ResumeLayout(false);
this.Resize += new System.EventHandler(this.frmGraphics_Resize);

Then it will work okay.

So ... what is the catch??? :/ Does anyone have any idea?
Well, I would have to see the code for UpdateControls to be sure, but
let me hazard a guess.

Designer-generated code is run in a Form's constructor. At that point
in the process, the form doesn't have a window handle yet. That is, it
does not yet correspond to a real window on the screen. As such,
certain operations are illegal, and certain properties that don't
normally return null do in fact return null.

The way that the Designer wrote the original code:

this.Resize += new System.EventHandler(this.frmGraphics_Resize);
this.ResumeLayout(false);

I'm guessing that the ResumeLayour call causes the Form to arrange the
positions and sizes of controls on its surface and, perhaps, resize
itself accordingly. Since the Designer has already attached an event
handler for the Resize event, this may cause your frmGraphics_Resize
method to be called when ResumeLayout runs. Therefore, your method is
being called before the window handle is allocated, and maybe you're
trying, in effect, to use some property that doesn't have a value yet.

Again, I'm guessing, but I would suppose that the second ordering:

this.ResumeLayout(false);
this.Resize += new System.EventHandler(this.frmGraphics_Resize);

would cause all of the mucking about with control positioning and
window sizing to happen before the frmGraphics_Resize method subscribes
to the Resize event. So, the method will miss the first Resize (the
one that happens during construction when the window is sort of
half-built) and handle the second and subsequent Resizes (which
probably happen only after the window is loaded and so has a proper
window handle, etc).

All of this, of course is pure supposition on my part. The way to find
out if I'm right or just blowing smoke is to put a try...catch around
the UpdateControls call, like this:

try
{
UpdateControls();
}
catch (NullReferenceException nre)
{
MessageBox.Show(String.Format("UpdateControls caused exception:
{0}", nre));
}

This should show you a message box pinpointing the exact line where the
exception occurs. I think that you'll find that you're doing some
operation or using some property that isn't valid until the window is
fully constructed, during the Load method. If you find that that's the
case, you could always try this:

private void frmGraphics_Resize(object sender, EventArgs e) {
if (this.IsHandleCreated) {
UpdateControls();
}
}

to see if it sorts things out.

Dec 24 '06 #2
To be true need to see code in "UpdateControls". I am sure you might
have added some extra code to the genarated code.

"Object reference not set to an instance of Object" .... Try new of
the object before using the object.

also

Compile the code in "Any CPU" mode.

Thanks
-Srinivas.
Nick Valeontis wrote:
I was writing a C# program this week. The program works okay in my pc (in
both debug and release builds) but realized that it would not work to other
pcs. It simply would close after displaying an "This program has encountered
an error etc etc etc.." popup.

I added some try, catch blocks and found that the error message was: "Object
reference not set to an instance bla bla". After spending some time to
locate the line that causes the problem, I again realized that the code was
generated by the designer! And .. that I have no idea at all of what is
really happening. The lines that cause the problem are:

this.Resize += new System.EventHandler(this.frmGraphics_Resize);
this.ResumeLayout(false);

frmGraphics function is:

private void frmGraphics_Resize(object sender, EventArgs e) {
UpdateControls();
}

UpdateControls is self explanatory. It checks if text in various controls of
the form should be updated.

Can anyone tell me why this code generates this error?

Note that if I change the lines

this.Resize += new System.EventHandler(this.frmGraphics_Resize);
this.ResumeLayout(false);

to

this.ResumeLayout(false);
this.Resize += new System.EventHandler(this.frmGraphics_Resize);

Then it will work okay.

So ... what is the catch??? :/ Does anyone have any idea?

-nick
Dec 24 '06 #3
Thanks!

You have really helped me isolate the problem.

I tend to add custom code after the call InitializeComponent() in the class
creator:

(Designer):
> this.Resize += new System.EventHandler(this.frmGraphics_Resize);
this.ResumeLayout(false);
public frmGraphics(AStar newastar, string title) {
InitializeComponent();
astar = newastar;
...
}
private void frmGraphics_Resize(object sender, EventArgs e) {
UpdateControls();
}
}
private void UpdateControls() {
DrawPathsAndText();
UpdateErrorProvider();
}
private void DrawPathsAndText() {
using (Graphics g = pnlGraphics.CreateGraphics()) {
g.TranslateTransform(0, pnlGraphics.Height);
g.ScaleTransform((float)m_scale, -1 * (float)m_scale);

uxPrev.Enabled = uxFirst.Enabled = (m_index 0);
uxNext.Enabled = uxLast.Enabled = (m_index < astar.Output.History.Count -
1);
...
...
}
}

So, what you describe makes sense here.

InitializeComponent calls UpdateControls, which calls DrawPathsAndText,
which tries to access properties of the astar instance. But astar has the
value of null, since the line "astar = newastar;" is not executed until
after InitializeComponent() is finished.

If I swap the lines in the frmGraphics creator:

public frmGraphics(AStar newastar, string title) {
astar = newastar;
InitializeComponent();
...
}
It works okay.

So, am I doing anything that is considered "bad practice" here?

Also, I would wonder. Why the error message does not appear when I run the
app in development pc?

Anyway, thanks again!
-Nick
Dec 25 '06 #4

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

Similar topics

2
by: TR | last post by:
Hi All: I have a simple test GUI that has 2 panels (a left side and a right side), and each panel is displaying an image. The left panel has a hardcoded size of (600,400) using the Dimension()...
2
by: Ivan Voras | last post by:
I hava a hierarhical sizer layout in which there's a panel in the upper part of a window with some buttons, and another panel with wxVListBox that's meant to occupy all the remaining space in the...
1
by: Terry | last post by:
I've seen several posts from people who have seen this flashing in TreeView's when resizing a form. I've noticed it in my app, but only in the child windows. For example, my main form has a...
8
by: Chris | last post by:
Hi, In design mode I built some windows with some controls (e.g. listboxes, labels, chgeck boxes etc.) in it, and I did set the property for the window size is set to normal. Now, when I run...
13
by: Martin Ho | last post by:
I know this must be trivial for many of you. But I am playing with this and can't figure it out. I have a form, on that form is one panel which has 3 textboxes, when I run my program and...
2
by: Rich | last post by:
Hello, If I place a panel control on a form and place a datagrid on the panel, is there a property for the panel to resize with the form? I am sure there is. I just can't remember what the...
2
by: tjfdownsouth | last post by:
I am using a panel inside an asp.net 2.0 page so that I can have the scrolling capabilities. Inside the panel I have a datagrid that I would like to stay within a certain area. On the initial...
3
by: marfi95 | last post by:
My application consists of basically a treeview on the left side, along with a panel control that takes up the right side. When nodes are clicked on the left, the data on the right side of the...
1
by: =?Utf-8?B?U3RldmUgUmFuZGFsbA==?= | last post by:
I have a form with a number of panel controls on it. I have overriden the paint event (of the panel controls) to provide custom painting. What I have done is to use the Paint event to paint the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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
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,...

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.