Reputation: 2381
I have a fairly simple piece of code:
Private _PurchaseDelivery as PurchaseDelivery
Protected Overrides Sub InsertItem(ByVal index As Integer, ByVal item As PurchaseDeliveryItem)
Dim SKUBin As SKUBin
If _PurchaseDelivery IsNot Nothing AndAlso _PurchaseDelivery.DefaultSKUBinID.HasValue Then
SKUBin = item.StockOrderUnit.SKU.SKUBins.GetByBinID(_PurchaseDelivery.DefaultSKUBinID.Value)
item.SKUBin = SKUBin
End If
MyBase.InsertItem(index, item)
End Sub
Which is inside a class which overrides a custom list base. The code is occassionaly throwing an unhandled exception, System.NullReferenceException
, on this line when used in production:
If _PurchaseDelivery IsNot Nothing AndAlso _PurchaseDelivery.DefaultSKUBinID.HasValue Then
DeafultSKUBinID is declared as an Integer?
(Nullable Int) in the PurchaseDelivery
class. I cannot see what might be causing this error, why would this be returning an error?
Upvotes: 0
Views: 513
Reputation: 2381
I eventually found the answer to my own problem. The class causing the error was returning the wrong line number in the exception. I have no idea why.
Any exception in this file returns a line number that it 1 less that the code the caused the exception, so the null reference is on the next line. I have no idea why this occurs, I have removed all whitespace in the file and recompiled and will see if that causes the issue to go away.
Thanks to everyone for their help.
Upvotes: 0
Reputation: 94645
As per code (In OP) snippet PurchaseDelivery
in not instantiated (may be it was created somewhere). You may try to change the if statement like:
Protected Overrides Sub InsertItem(ByVal index As Integer, ByVal item As PurchaseDeliveryItem)
Dim SKUBin As SKUBin
If IsNothing(_PurchaseDelivery) Then
Exit Sub
End If
If IsNothing(_PurchaseDelivery.DefaultSKUBinID) Then
Exit Sub
End If
If _PurchaseDelivery.DefaultSKUBinID.HasValue Then
SKUBin = item.StockOrderUnit.SKU.SKUBins.GetByBinID(_PurchaseDelivery.DefaultSKUBinID.Value)
item.SKUBin = SKUBin
End If
MyBase.InsertItem(index, item)
End Sub
Upvotes: 1
Reputation: 12804
_PurchaseDelivery.DefaultSKUBinID is nothing.
Private _PurchaseDelivery as PurchaseDelivery
Protected Overrides Sub InsertItem(ByVal index As Integer, ByVal item As PurchaseDeliveryItem)
Dim SKUBin As SKUBin
If _PurchaseDelivery IsNot Nothing AndAlso _PurchaseDelivery.DefaultSKUBinID isnot nothing andalso _PurchaseDelivery.DefaultSKUBinID.HasValue Then
SKUBin = item.StockOrderUnit.SKU.SKUBins.GetByBinID(_PurchaseDelivery.DefaultSKUBinID.Value)
item.SKUBin = SKUBin
End If
MyBase.InsertItem(index, item)
End Sub
Upvotes: 1