Windows Develop Bookmark and Share   
 index > Windows Forms General > Why does it say I have a nullReference?
 

Why does it say I have a nullReference?

Hello,

I am having a hard time deciphering why the IDE will give me a nullReference error when assigning a text string to a textBox.Text control. Following is the code (red highlight is the code where it'd say NullReferenceException unhandled). The debugger says this.TextBox1 = null. I compared this with other codes that I have doing the same thing and in those cases, the debugger would say textBox2 = {Text = ""}.

Any ideas why? In both cases, I right click on it and selected Find All References. I can see that both have the following:

- a line that declares the control

- a line that instantiate the control

- a line that adds the control to the form

- a few lines that set the properties of the control

and

- the line that assigns a string to it.

public partial class InformationBox : Form
{
public InformationBox(string displayInfo)
{
//textBox1.Text = displayInfo;
this.textBox1.Text = "hello";
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
this.Close();
}

public delegate void ControPass();
public ControPass controlPass;

private void button2_Click(object sender, EventArgs e)
{
if (controlPass != null)
{
controlPass();
}
this.Close();
}
}

amNotAGeek  Sunday, August 26, 2007 10:03 AM

Switch the code around - place InitializeComponent() method BEFORE setting the property of the textbox. It needs to initialize the controls on the form before you are allowed to access the controls
ahmedilyas  Sunday, August 26, 2007 11:53 AM

Switch the code around - place InitializeComponent() method BEFORE setting the property of the textbox. It needs to initialize the controls on the form before you are allowed to access the controls
ahmedilyas  Sunday, August 26, 2007 11:53 AM

Hello,

OMG. Can't believe I didn't notice that. Thanks Mr. MVP.

amNotAGeek  Sunday, August 26, 2007 3:38 PM

You can use google to search for other answers

Custom Search

More Threads

• uploading files to a web server
• Does a Java BorderLayout-like thing exist in C++?
• VS 2005 IDE Crash
• chat server coding console based how change to windowsapplication form?
• Autocomplete in multiline textbox
• Disable Cut and Paste
• Control.Invoke & Delegates in VB
• Control.DrawToBitmap and Performances
• Web Browser Controls - Enter Key
• Seeking code showing SQL 2008 Heirarchy ID data bound to a tree control