我想問我如何可以包含我得到的錯誤「對象引用未設置爲對象的實例。」,nullreferenceException。我試圖分配我得到的值從gridview到一個變量。我在第一行發現錯誤。當值爲null時,我該如何處理這種情況。我嘗試過使用isnot,但它仍然給我錯誤,並且isdbnull不會工作,因爲我沒有處理數據表。如何處理nullreferenceexception
我只有當gridview仍然是空的時候出現這個錯誤我該如何處理。
If Not IsNothing(ProductsRawMaterialGrid.GridViewElement.CurrentRow.Cells("PercentageInMix").Value) Then
PIM = ProductsRawMaterialGrid.GridViewElement.CurrentRow.Cells("PercentageInMix").Value
Else
PIM = FormatNumber("0.00", 2)
End If
[使用VB.NET IIF的可能的重複我得到NullReferenceException](http://stackoverflow.com/questions/428959/using-vb-net-iif-i-get-nullreferenceexception) – Stefan 2012-08-01 09:01:19
不完全重複,但在另一個問題上接受的答案將完全適用於這個問題。 – Stefan 2012-08-01 09:02:27
也許你的問題開始於當網格爲空時的CurrentRow。這是失敗的對象引用。 – Steve 2012-08-01 09:05:25