2012-05-18 38 views
0

我在这里有一个项目在我的ojt。 vb2010表单。我得到这个错误:“NullReferenceExeption was unhandled”..为什么我得到它?我希望任何人的帮助。谢谢。“NullReferenceExeption was Unhandled”

这是我的代码;

Private Sub RefreshData() 
    If Not con.State = ConnectionState.Open Then 

     con.Open() 
    End If 

    Dim da As New OleDb.OleDbDataAdapter("SELECT ID as [ID], " & _ 
             "fname as [NAME], lname" & _ 
             "FROM asdf ORDER by ID", con) 

    da.Fill(ds.Tables("asdf"))**** this part is where i get the error***** 
    con.Close() 
End Sub 
+0

最有可能的,'ds'并没有被您试图访问其'Tables'收集的时间实例化。 “ds”甚至在哪里申报? –

+0

你能提供一些关于如何解决这个问题的提示吗?我只是一个新手而已。这是我的第一个项目。 –

回答

1

错误说是什么意思,

把空校验

if(ds != null && ds.Tables("asdf") != null) then 
.... ' put da.Fill here 
end if 
0

NullReferenceExeption是未处理的”错误表明您试图在代码中使用空对象。做一个像null check -

if(con != null) then 
     'your code... 
end if 


if(ds != null && ds.Tables("asdf") != null) then 
    'your code... 
end if 
+0

我试过你的代码先生,但我在“=”符号上生了一个“”标识符预期“”。 –

+0

我用它,但我得到了“&&”符号的“”表达预期“”错误。是的,我使用vb2010。我只是vb中的新手。这只是我的第一个应用程序。 –

+0

只需写'And'来代替'&&'操作符。上面的答案只描述了逻辑,而不是语法。对于和VB使用'和'。 – Jaguar

相关问题