VB NullReferenceException 未处理

VB NullReferenceException was unhandled

我知道这里有此标题下的文章,但我无法将它们与我的错误联系起来。我试图拼凑一个模拟查询页面作为 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 服务器的连接检查。有人发现了什么吗?

不知道这是否应该是答案或评论,但是...错误非常明显。

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

您正在将 'dgv_QueryData' 初始化为 'Nothing',然后立即使用它,试图访问它的 'DataSource' 属性...您必须将其初始化为某些东西,另一个变量或 'New '...