Link to home
Start Free TrialLog in
Avatar of dpdmembers
dpdmembersFlag for Barbados

asked on

NullReferenceException Problem

I am retrieving some data form a table using linq.  I am pulling that info into text boxes I have on a form.  But because some of the fields may be null and there is no way to know which ones will be null at any given time, thus I keep getting a NullReferenceException or an Object reference not set to an instance of an object error.  I have tried everything to try to test the field to see if it is null but nothing has work.
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

can you show us some code for which you have issues?

are you using VS2015 and targeting one of the last Framework? If yes, you might want to try the Elvis operator: https://msdn.microsoft.com/en-us/library/dn986595.aspx
Avatar of dpdmembers

ASKER

I am using VS2012.

 Dim xNo = (From xReg In _Context.tbl_Peoples
                          Where xReg.str_Nat = Me.NatTextBox.Text
                          Select xReg)

If xNo.Count > 0 Then
            For Each NatReg In xNo

                NameTextBox.Text = NatReg.str_ForeName2

            Next

End If


If NatReg.str_ForeName2 is null in the database table I get the errors.
can you try:
if string.isnullorwhitespace(NatReg.str_ForeName2) then
   NameTextBox.Text = string.empty
else
   NameTextBox.Text = NatReg.str_ForeName2
end if

Open in new window

I tried that already.  Does not even get past the IF.

I got the error Object reference not set to an instance of an object.
attached file For your review
capture.png
ASKER CERTIFIED SOLUTION
Avatar of Éric Moreau
Éric Moreau
Flag of Canada 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
Looks promising..will test it over the weekend and get back to you.  Thanks.