vb.net表单linq nullreferenceexception

vb.net表单linq nullreferenceexception,vb.net,linq-to-sql,Vb.net,Linq To Sql,我正在开发一个Sindows表单应用程序来帮助保存一些扫描仪的清单。我使用的是Linq2Sql,每个表都有一个id列。在我的维修记录表上。我试图使用库存表中的序列号,因此它会进入数据库,并从表中查找sID,然后返回正确的值,但当我将所有输入的数据发送到历史表时,它会收到一个空引用异常 Dim db作为新的DataClasses1DataContext 作为扫描仪的Dim rep\u修复\u历史记录 Dim scan=(从db.Scanner\u清单中的Scanner\u清单,其中scanneri

我正在开发一个Sindows表单应用程序来帮助保存一些扫描仪的清单。我使用的是Linq2Sql,每个表都有一个
id
列。在我的维修记录表上。我试图使用库存表中的序列号,因此它会进入数据库,并从表中查找sID,然后返回正确的值,但当我将所有输入的数据发送到历史表时,它会收到一个空引用异常

Dim db作为新的DataClasses1DataContext
作为扫描仪的Dim rep\u修复\u历史记录
Dim scan=(从db.Scanner\u清单中的Scanner\u清单,其中scannerid.Text=Scanner\u Inventory.SN选择Scanner\u Inventory.SID)。FirstOrDefault
rep.SID=扫描
rep.Date\u break=datebreak.Value
rep.Description=Description.Text
rep.Send\u Date=senddate.Text
rep.receive\u Date=receivedate.Text
代表成本=成本文本
rep.PlantID=PlantID.Text
rep.BID=brokenid.Text
rep.RMAnumber=rmanum.Text
db.扫描仪\修复\历史记录。InsertOnSubmit(代表)
db.SubmitChanges()

是我,但你没有实例化你的“rep”变量

你没有定义一个带有“new”关键字的放置对象,但我也想知道它是否是system.type

基于Jinx88909的更新 您可能返回一个完整的POCO对象,该对象可能为null并且具有null属性。如果您使用的是.NET 4.5及更高版本,则大多数情况下可以通过执行空条件来调整此设置。“?”接线员

Dim db As New DataClasses1DataContext
'I need to be a new object and not instantiated as Nothing
Dim rep As New Scanner_Repair_History

'You have the potential for a nothing value here as 'FirstOrDefault' includes a potential Nothing'. 
'I would get the entire object and then just a property of it after the fact
Dim scan = (From Scanner_Inventory In db.Scanner_Inventories Where scannerid?.Text = Scanner_Inventory?.SN Select Scanner_Inventory).FirstOrDefault?.Sid 

If scan IsNot Nothing Then
  rep.SID = scan 'Could you maybe want scan.Id or something similar?
  rep.Date_Broken = datebroke.Value
  rep.Description = description.Text
  rep.Send_Date = senddate.Text
  rep.Recieve_Date = recievedate.Text
  rep.Cost = cost.Text
  rep.PlantID = plantid.Text
  rep.BID = brokenid.Text
  rep.RMAnumber = rmanum.Text

  db.Scanner_Repair_Histories.InsertOnSubmit(rep)
  db.SubmitChanges()
Else
  Console.WriteLine("I got nothing for you with the inputs you put in!")
End If

使用isnot nothing的可能的重复只是跳转到表中的插入。从我启动程序时可以看出,程序“rep”与它的假设一样指向“Scanner\u Repair\u History”表,但当它跳转到查询“rep”之后,它会变为“nothing”。我不完全确定原因是什么。“扫描”确实返回了扫描序列号的正确id