3

Curious as to why I'm getting this error when attempting to debug my program in VB.net

Object Reference not set to an instance of the object.

It says that I am receiving this error due to lines 4 and 5:

Public Class Form1

Dim tSize
Dim S1 As String = ComboBox1.Text
Dim S2 As String = ComboBox2.Text

Private Sub FitContents()
    tSize = TextRenderer.MeasureText(TextBox3.Text, TextBox3.Font)
    TextBox3.Width = tSize.Width + 10
    TextBox3.Height = tSize.Height
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    TextBox1.Text = S1
    TextBox2.Text = S2
End Sub

Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.TextChanged
    Call FitContents()
End Sub

End Class

I would be extremely grateful if somebody were to explain why I am receiving this error.

1
  • 1
    tSize does not have a type specified. Is this intentional? I suspect that you do not have Option Strict turned on. Do yourself a favor and turn Option Strict On. Commented Dec 21, 2012 at 15:56

1 Answer 1

4

Class level variables like S1 and S2 are initialized very early in the object construction process. Your visual controls, like Combobox1 and Combobox2, are not created until the InitializeComponent() method is called, which is not until almost at the end of the constructor.

Therefore, at the point when you try to set S1 to the value of Combobox1.Text, the Combobox1 object has not been created yet, and tring to reference a Null object's .Text property is going to give you that exception.

Instead, set those values at the end of your constructor, or in response to an event like Load.

You can also try building them as a property... like this:

Private Property S1() As String
    Get
        Return ComboBox1.Text
    End Get
    Set (ByVal value As String)
        ComboBox1.Text = value
    End Set
End Property
Sign up to request clarification or add additional context in comments.

5 Comments

How would I set the order of things in the constructor? Thanks for the answer.
Move the lines of the code to the bottom of your constructor. The constructor is located in another file (the designer file), which is why I also recommended using the Load event.
I tried to put Dim S1 As String = ComboBox1.Text at the end of the constructor, but I am still receiving the error. I also attempted to define the variables in the Form_load event, but that didn't work either.
Don't define the variables there... just do the assignment there. Also, have you considered building them as a property? I'll edit that into my answer so you can see the code snippet.
Wow I feel stupid now. Thank you so much for helping me learn from my mistakes, I'm quite new to Visual Basic.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.