Sign in to follow this  
awesty

[C#] NullReferenceException was unhandled?

Recommended Posts

awesty    124
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
jpetrie    13099
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
nobodynews    3126
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

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