VB.NET-更改Textbox的.Text属性会抛出Null Reference Exception

时间:2012-08-16 04:34:09

标签: nullreferenceexception vb.net-2010

这必须是微不足道的。我以此为生,无法弄清楚为什么我会得到这个例外:

  

System.NullReferenceException未处理Message = Object引用   没有设置为对象的实例。

以下是代码:

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

结束班

帮助?

1 个答案:

答案 0 :(得分:2)

这是你改变它几次后的吗?

设置txtTaxRate = Nothing以及稍后尝试将txtTaxRate.Text设置为其他内容会导致一些问题。

您正在将文本框对象设置为空,然后尝试引用其中一个属性。

尝试更改

txtTaxRate = Nothing

txtTaxRate.Text = ""