Link to home
Start Free TrialLog in
Avatar of AlexPonnath
AlexPonnathFlag for United States of America

asked on

VB.NET System.NullReferenceException

I was wondering how i can handle / catch any property in a Data Class which is not initialized ?
I know i can set it to "" when i create a new instance but i still would like to handle the possible exception.

In my case i loop thru the data values in my data Class and when i get to an item that is not initialized i get
the System.NullReferenceException.

Below is the code i use so far
   Dim myQueryString As String = ""

        Dim _type As Type = myData.GetType()
        Dim properties() As PropertyInfo = _type.GetProperties()
        For Each _property In properties

            If _property.GetValue(myData, Nothing).ToString IsNot Nothing Then
                If _property.GetValue(myData, Nothing).ToString <> "" Then

                    myQueryString += "" & _property.Name & ": '" & _property.GetValue(myData, Nothing).ToString & "' ,"

                End If
            End If
        Next

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Nitin Sontakke
Nitin Sontakke
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial