Link to home
Create AccountLog in
Avatar of Mr_Fulano
Mr_FulanoFlag for United States of America

asked on

'System.NullReferenceException' problem.

Hi, I have a Function that looks inside a DLL using "Reflections" and gets me the Form.Tag property for that DLL. The code works beautifully as long as there IS a value for it to get.  In other words, if the value is Null, I get the 'System.NullReferenceException' - 'Object reference not set to an instance of an object' error. I tried wrapping the code in an IF statement (please see code snippet below) to check for a DBNull.Value, but I still get the error. Is there a better way to check if the value is Null, or am I going to have to use a Try/Catch statement?

I know that the code works as long as the Tag value is present. It gives me the error if it is Null.

The line of code below is the one that gives me the error when the Form.Tag value is Null.

Dim sTag As String = myProperty.GetValue(myForm, Nothing).ToString()

Any thoughts?

Thanks,
Fulano

If (myProperty.GetValue(myForm, Nothing).ToString()) IsNot DBNull.Value Then
   Dim sTag As String = myProperty.GetValue(myForm, Nothing).ToString()
   Debug.WriteLine("The TAG Name is  :" & sTag)
Else
   Debug.WriteLine("none")
End If

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of gnoon
gnoon
Flag of Thailand image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of Mr_Fulano

ASKER

Sometimes things are right in front of your nose and you can't see them...

You're both correct. Thanks for the help!!!  I increased the points and split them evenly.

Fulano