Sign in to follow this  
awesty

[C#] NullReferenceException was unhandled?

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
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

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