Sign in to follow this  

[C#] NullReferenceException was unhandled?

This topic is 3781 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
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

This topic is 3781 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this