2013-04-18 90 views
-1

這是我的代碼。這是我第一次在vb.net上。NullReferenceException不受管理。 「表」不返回任何內容。爲什麼?

Private Sub Accountnum_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Accountnum.KeyPress 
    Dim index As Long = 0 
    If Asc(e.KeyChar) = 13 Then 
     Do While index <= 111000 
      If Accountnum.Text = ds.Tables("dbo.AccountMaster").Rows(index).Item("AccountNumber").ToString Then 
       Nameconsumer.Text = ds.Tables("dbo.AccountMaster").Rows(index).Item("ConsumerName") 
       Address.Text = ds.Tables("dbo.AccountMaster").Rows(index).Item("ConsumerAddress") 
      End If 
     Loop 
    End If 

End Sub 
+0

在哪一行是錯誤? – sashkello 2013-04-18 03:06:13

回答

0

Null引用錯誤僅僅意味着您正試圖對尚未設置其值的對象採取某些操作。人們在使用帶有方便的點符號的語言時忽略這些錯誤的來源並不罕見。

看看像你摘錄行5一個例子: ds.Tables( 「dbo.AccountMaster」)行(指數).Item( 「賬戶號碼」)的ToString

這是沒有的。不常見,但你在這裏連接四個參考,並假設它們都是有效的。查看發生錯誤的行,並檢查語句中的每個組件的值。在上面的線,你會要檢查: - DS - ds.Tables( 「dbo.AccountMaster」) - ds.Tables( 「dbo.AccountMaster」)行(指數) - 等

。如果其中任何一個爲空,那麼您嘗試對其結果進行後續操作,例如ToString將導致Null引用異常。

+0

添加到此。有時最好寫很多行,直到你知道哪些行可以被鏈接,哪些可能需要錯誤檢查。 – 2013-04-18 03:15:28

相關問題