Why NullReferenceException warning inside try...catch?

In the code snippet in the attached file, nothing I tried would remove the NullReferenceException warning on vehicle.SelectSingleNode("f[@n='DLRStockID']").

I would think rapping the block in a try...catch would have been sufficient to remove that warning.

What am I missing?

Thanks,
David Holt



Attachment(s):
NullReferenceException.cs.zip
1 comment

The try/catch handles the exception, but does not prevent it.  To prevent it, check the return value of vehicle.SelectSingleNode("f[@n='DLRStockID']") for null.

    var n = vehicle.SelectSingleNode("f[@n='DLRStockID']");
    if(n == null)
    {
        stockId = "N/A";
    }

0

Please sign in to leave a comment.