0

I have a class someone else wrote for which I don't have the source code. It has a property UM that is backed by _UM, a string. In some circumstances _UM is Nothing. I would expect that UM would be Nothing too, but when I inspect (using Quick Watch) the property it shows as a NullReferenceException. When I try testing for Nothing I get a NullReferenceException thrown in my main code. How can I catch this condition so I can handle it properly?

If Foo.UM Is Nothing Then
    DoSomething()
End If

...throws a NullReferenceException.

3
  • 2
    The exception may be thrown because Foo is Nothing. Commented Apr 17, 2012 at 20:48
  • Foo isn't nothing. Other properties I can see. Commented Apr 17, 2012 at 20:53
  • try ... catch NullReferenceException ... Commented Apr 17, 2012 at 21:01

1 Answer 1

1

The property might do more than just return the _UM field. Probably uses it somehow and does not take into account that it might be null. You could do something like this to handle it:

Dim obj = Nothing
Try
    obj = Foo.UM
Catch ex As NullReferenceException
End Try
If obj Is Nothing Then
    DoSomething()
End If
Sign up to request clarification or add additional context in comments.

3 Comments

I would agree that the property probably does something besides just return _UM, however, I don't particularly care. I just need to test and take action. Any thoughts on how to do that?
I was hoping to avoid that as it is quite a bit of overhead to process an exception...oh well. This class is quite lousy in other areas, I shouldn't be surprised. Thanks for your suggestion.
You can always decompile the external dll with Reflector for example and see what is actually going on in the property.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.