2012-08-16 42 views
0

這必須是微不足道的。我這樣做是爲生活和想不通爲什麼我收到此異常:VB.NET-文本框的更改.Text屬性拋出空引用異常

System.NullReferenceException了未處理消息=對象引用 不設置到對象的實例。

下面是代碼:

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

在哪裏拋出的異常? – 2012-08-16 04:36:10

+0

查看代碼中的評論。具體來說,它被引用到select case語句中,引用txtTaxRate TextBox – 2012-08-16 04:37:06

+1

這是在你改變了幾次之後?設置txtTaxRate = Nothing,稍後嘗試將txtTaxRate.Text設置爲其他內容會導致一些問題。嘗試更改txtTaxRate = Nothing to txtTaxRate.Text =「」 – 2012-08-16 04:38:15

回答

2

這是你改變它幾次後?

設置txtTaxRate = Nothing和稍後嘗試將txtTaxRate.Text設置爲別的會導致一些問題。

您正在設置文本框對象爲空,並稍後嘗試引用其屬性之一。

嘗試改變

txtTaxRate = Nothing

txtTaxRate.Text = ""