2013-04-18 87 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

相关问题