• Advertisement
Sign in to follow this  

[C#] NullReferenceException was unhandled?

This topic is 3849 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a text box in my form called textBox1. I am trying to make it say "Hello" by this line of code: textBox1.Text = "Hello"; But it just tells me that NullReferenceException was unhandled. Anyone know what I am doing wrong? Thankyou. ;)

Share this post


Link to post
Share on other sites
Advertisement
Either: textBox1 is null (are you attempting to assign the string before you called InitializeComponent() in the constructor?)

Or: something else is null.

In either case you will benefit from running the program in the debugger and stepping through to actually isolate the line of code that throws the null reference exception. You'll also want to post more code if you can't figure it out on your own.

Share this post


Link to post
Share on other sites
Did you run the code through the debugger? Determine the exact line of code it crashes on? Did you check the value of textBox1 before the crashed line of code? Right now, I'm guessing that you are assigning "Hello" to textBox1.text at a time when textBox1 has not been initialized. Without more information I don't think anyone can tell for sure.

Share this post


Link to post
Share on other sites
I feel so stupid now. I put the line of code before InitializeComponent().

*Hits himself in the back of the head*

Share this post


Link to post
Share on other sites
No biggy. How do you think I knew what you did? I obviously did it to myself some time. You learn from your mistakes, and then in the future when you have a problem like this you will remember.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement