在我的datacontext,CalcinerDataContext中,我有一个类,MainData和一个简单继承的类NewSample(用于探索此错误)。下面的代码运行没有任何错误:
Dim cdc As New CalcinerDataAccess.CalcinerDataContext
Dim newData As New CalcinerDataAccess.MainData
With newData
.LotNumber = "Test1"
.ProcessType = 4
.SubmitterID = 2
.Calciner = 1
.DateEntered = Now
End With
Try
cdc.MainDatas.InsertOnSubmit(newData)
Catch ex As Exception
Debug.WriteLine(ex.ToString)
MsgBox(ex.Message)
End Try
以下代码会生成以下错误: System.NullReferenceException:未将对象引用设置为对象的实例。 在System.Data.Linq.Table`1.InsertOnSubmit(TEntity实体)。请注意,这两个代码块之间的唯一区别是newData
的实例化 Dim cdc As New CalcinerDataAccess.CalcinerDataContext
Dim newData As New CalcinerDataAccess.NewSample
With newData
.LotNumber = "Test1"
.ProcessType = 4
.SubmitterID = 2
.Calciner = 1
.DateEntered = Now
End With
Try
cdc.MainDatas.InsertOnSubmit(newData)
Catch ex As Exception
Debug.WriteLine(ex.ToString)
MsgBox(ex.Message)
End Try
为什么继承的类会引发异常,但基类不会?