对象引用未设置为对象VB的实例

时间:2014-02-13 07:53:41

标签: vb.net

我有一个错误,我无法摆脱(在我复制的代码的末尾)。我读到了它并且我理解了什么是在说什么,但是我在该行中使用的两个对象都是实例化的并且之前使用过。我使用了断点,在我看来两者都有值,它们与null不同。例如,修改或删除操作按预期执行(我没有在此处复制它们)。你能帮助我吗。谢谢!

Public Class Form1
    Dim dataSet As DataSet
    Dim dataAdapterStudenti As New System.Data.SqlClient.SqlDataAdapter
    Dim dataAdapterSectii As New SqlClient.SqlDataAdapter

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Try
            con.Open()
            dataSet = New DataSet

            Dim insertStudenti As New SqlClient.SqlCommand
            insertStudenti.CommandType = CommandType.Text
            insertStudenti.CommandText = "insert into studenti (cods,grupa,nume,datan,nrmatricol) values (@cods,@grupa,@nume,@datan,@nrmatricol)"
            insertStudenti.Connection = con
            insertStudenti.Parameters.Clear()
            p = insertStudenti.Parameters.Add("@cods", SqlDbType.Int, 4, "cods")
            p.SourceVersion = DataRowVersion.Current
            p = insertStudenti.Parameters.Add("@nrmatricol", SqlDbType.Int, 4, "nrmatricol")
            p.SourceVersion = DataRowVersion.Current
            p = insertStudenti.Parameters.Add("@nume", SqlDbType.VarChar, 40, "nume")
            p.SourceVersion = DataRowVersion.Current
            p = insertStudenti.Parameters.Add("@grupa", SqlDbType.Int, 4, "grupa")
            p.SourceVersion = DataRowVersion.Current
            p = insertStudenti.Parameters.Add("@datan", SqlDbType.DateTime, 4, "datan")
            p.SourceVersion = DataRowVersion.Current
            dataAdapterStudenti.InsertCommand = insertStudenti

            dataAdapterStudenti.Fill(DataSet, "studenti")
            DataSet.Tables("studenti").Constraints.Clear()
            DataSet.Tables("studenti").Constraints.Add("PK_nrmatricol", DataSet.Tables("studenti").Columns("nrmatricol"), True)

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub   

    Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
        If (Verifica() And EsteNrMatricolUnic()) Then
            Dim dr As DataRow
            dr = dataSet.Tables("studenti").NewRow()
            dr("cods") = CInt(txtCodS.Text)
            dr("nrmatricol") = CInt(txtNrMatricol.Text)
            dr("nume") = txtNume.Text
            dr("grupa") = txtGrupa.Text
            dr("datan") = pickerDataNasterii.Value
            Try
                dataSet.Tables("stdenti").Rows.Add(dr) 'here is the error
                dataAdapterStudenti.Update(dataSet, "studenti")
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End If
    End Sub
End Class

1 个答案:

答案 0 :(得分:2)

我怀疑错误的原因是一个简单的拼写错误(stdenti vs. studenti):

dataSet.Tables("studenti").Rows.Add(dr) 'here is the error

应该有效。如果向dataSet.Tables("invalidname")提供无效的表名,则它将返回null(VB.NET中为Nothing)。之后对Rows的调用会导致您遇到的异常。

也就是说,NullReferenceException指出您尝试在具有null(Nothing)值的引用上调用成员(方法,属性,...)。有关详细信息,请参阅此post