简体   繁体   中英

VB.NET- Changing .Text property of Textbox throws Null Reference Exception

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM