0

This MUST be trivial. I do this for a living and CAN NOT figure out why I am getting this exception:

System.NullReferenceException was unhandled Message=Object reference not set to an instance of an object.

Here is the code:

Public Class frmMain

Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim counties() As String

    counties = {"", "Huntsville, AL", "Madison, AL", "Rural Madison County, AL", "Taft, TN"}
    Me.cbCounties.DataSource = counties

    Me.lblStatus.Text = "[ Please select a county ]"
    Me.lblStatus.Left = Me.ClientSize.Width \ 2 - Me.lblStatus.Width \ 2



End Sub

Private Sub cbCounties_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbCounties.SelectedIndexChanged
    Select Case cbCounties.SelectedIndex
        Case 1
            txtTaxRate.Text = "8.00%" 'Issue is these, when index is changed.
        Case 2
            txtTaxRate.Text = "8.50%"
        Case 3
            txtTaxRate.Text = "5.50"
        Case 4
            txtTaxRate.Text = "9.50%"
        Case Else
            txtTaxRate.Text = Nothing
    End Select

    Me.lblStatus.Text = "[ Please enter net amount ]"
    Me.lblStatus.Left = Me.ClientSize.Width \ 2 - Me.lblStatus.Width \ 2

End Sub

End Class

Help?

4
  • 1
    Where is the exception thrown? Commented Aug 16, 2012 at 4:36
  • See comment inside of code. Specifically, it is thrown inside of the select case statement, referring to the txtTaxRate TextBox Commented Aug 16, 2012 at 4:37
  • 1
    Is this after you changed it a couple of times? setting txtTaxRate = Nothing and later trying to set txtTaxRate.Text to something else will cause some problems. try changing txtTaxRate = Nothing to txtTaxRate.Text = "" Commented Aug 16, 2012 at 4:38
  • Alright... that was it. Make this an answer and I will accept. Commented Aug 16, 2012 at 4:39

1 Answer 1

2

Is this after you changed it a couple of times?

Setting txtTaxRate = Nothing and later trying to set txtTaxRate.Text to something else will cause some problems.

You are setting the textbox object to nothing, and later trying to reference one of its properties.

Try changing

txtTaxRate = Nothing

to

txtTaxRate.Text = "" 
Sign up to request clarification or add additional context in comments.

Comments

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.