2016-11-25 7 views
-3

私は一部のスキャナの在庫を維持するのに役立つSindowsフォームアプリケーションに取り組んでいます。 Linq2Sqlを使用していますが、各テーブルにはidというカラムがあります。修理履歴書に私はインベントリテーブルのシリアル番号を使用してデータベースに行き、テーブルからsIDを検索して正しい値を返しますが、入力したすべてのデータを履歴テーブルに送信するとnull参照例外です。vb.net form linq nullreferenceexception

Dim db As New DataClasses1DataContext 
Dim rep As Scanner_Repair_History 

Dim scan = (From Scanner_Inventory In db.Scanner_Inventories Where scannerid.Text = Scanner_Inventory.SN Select Scanner_Inventory.SID).FirstOrDefault 

rep.SID = scan 
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() 
+2

[とNullReferenceExceptionは何ですか、と私はそれを修正しますか?](の可能性のある重複http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do- i-fix-it) – Bugs

答えて

0

私はしかし、あなたはあなたの「担当者」変数

0

をインスタンス化しませんでしたあなたは「新しい」キーワードで配置するために定義されたオブジェクトを持っていないが、それであれば、私はまた、好奇心旺盛だということですシステムタイプ。

Jinx88909に基づく更新 nullの可能性があり、nullプロパティを持つPOCOオブジェクト全体を返すことがあります。 .NET 4.5以降を使用している場合は、null条件を使用してこの時間を調整できます。 '?'オペレーター。

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 
+0

isnotを使ってテーブルに挿入するだけで何もジャンプしません。 私は 'rep'が 'Scanner_Repair_History'テーブルを指していると思いますが、 'rep'が 'nothing'に変更されたクエリを先にジャンプしたときに、プログラムを開始するときにわかります。何が原因なのか完全にわからない'scan'は、scaner SNの正しいIDを返します – Johnathan