Vb.net 对象引用未设置为对象的实例
我有一个无法摆脱的错误(在我复制的代码末尾)。我读过这篇文章,我理解了它的意思,但我在这行中使用的两个对象都是实例化的,以前也使用过。我使用过断点,在我看来,它们都有值,它们不同于null。例如,“修改”或“删除”操作按其应有的方式执行(我没有在此处复制它们)。你能帮助我吗。谢谢大家!Vb.net 对象引用未设置为对象的实例,vb.net,Vb.net,我有一个无法摆脱的错误(在我复制的代码末尾)。我读过这篇文章,我理解了它的意思,但我在这行中使用的两个对象都是实例化的,以前也使用过。我使用过断点,在我看来,它们都有值,它们不同于null。例如,“修改”或“删除”操作按其应有的方式执行(我没有在此处复制它们)。你能帮助我吗。谢谢大家! Public Class Form1 Dim dataSet As DataSet Dim dataAdapterStudenti As New System.Data.SqlClient.Sql
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
我怀疑错误的原因是一个简单的打字错误(stdenti vs.studenti): 应该有用。如果为dataSet.Tables(“invalidname”)提供了无效的表名,它将返回null(在VB.NET中为空)。之后对
行的调用
会导致出现异常
也就是说,NullReferenceException指出,您试图对具有null(Nothing)值的引用调用成员(方法、属性等)。详细信息请参见此图。我在过去三天里一直在看它。这太疯狂了:(()()))
dataSet.Tables("studenti").Rows.Add(dr) 'here is the error