Tamango
Tamango

Reputation: 3

Object reference not set to an instance of an object VB

I have an error that I can't get rid off (at the end of the code I've copyed). I read about it and I understand what is is saying, but the two objects that I'm using in that line are both instanciated and used before. I've used breakpoints and it seems to me that both have values, they are different from null. For example the modify or delete actions perform as they should (I didn't copy them here). Can you help me. Thank you!

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

Upvotes: 0

Views: 783

Answers (1)

Markus
Markus

Reputation: 22511

I suspect that the reason for the error is a simple typo (stdenti vs. studenti):

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

should work. If you supply an invalid table Name to dataSet.Tables("invalidname"), it will return null (Nothing in VB.NET). The call to Rows afterwards leads to the exception you experience.

That said, a NullReferenceException points out that you try to call a member (method, property, ...) on a reference that has a null (Nothing) value. See this post for details.

Upvotes: 2

Related Questions