VB NullReferenceException未处理

时间:2016-04-15 19:07:12

标签: vb.net nullreferenceexception

我知道这个标题下有文章,但我不能将它们与我的错误联系起来。我正在尝试拼凑一个模拟查询页面作为Uni任务的一部分,并偶然发现了我已经修改过的脚本。有两种形式,一种是对数据网格运行集合查询 - 这可以工作,但在下面的另一个页面脚本上有这个NullReferenceException。代码与工作表格完全相同,所以出了什么问题?

Public Class Adding_Information

'Dim SQL As New SQLControl
Private SQL As New SQLControl
Public Property DGV_SetQueries As Object



Private Sub btnCustomerFind_Click(sender As Object, e As EventArgs) Handles btnCustomerFind.Click
    If txtCustomerName.Text <> "" Then

        If SQL.HasConnection = True Then
            Dim dgv_QueryData As Object = Nothing
            dgv_QueryData.DataSource = SQL.SQLDataset.Tables(0)
            'Create a query variable
            Dim query As String = "Select
                              Customer.Title,
                              Customer.Firstname,
                              Customer.Surname,
                              Customer.Email,
                              Customer.MobileNo,
                              Customer.Postcode,
                              Customer.Regno
                            From
                              Customer
                            Where
                              Customer.Surname = 'Fletcher'
                            Order By
                              Customer.Firstname"

            SQL.RunQuery(query)

            If SQL.SQLDataset.Tables.Count > 0 Then

                'ensure that there is only one table
                DataGridView1.DataSource = SQL.SQLDataset.Tables(0)

            End If

        End If

    End If

End Sub

Private Sub DGV_SetQueries_CellContentClick(sender As Object, e As DataGridViewCellEventArgs)

End Sub

Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick

End Sub
End Class

DataGridView被称为datagridview1hasconnection是对SQL服务器的连接检查。有没有人发现过什么?

1 个答案:

答案 0 :(得分:2)

Dunno,如果这应该是一个答案或评论,但......错误很明显。

    If SQL.HasConnection = True Then
        Dim dgv_QueryData As Object = Nothing
        dgv_QueryData.DataSource = SQL.SQLDataset.Tables(0)

您正在初始化&#39; dgv_QueryData&#39;没有什么&#39;然后立即使用它,试图访问其数据源&#39;属性...你必须将它初始化为某个东西,或者是另一个变量,或者是新的&#39; ...