Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all,

I have a small problem. The thing is, how to handle a NullReferenceException when accessing foreign key classes, in other words, check if the foreign key instance is null before calling / using it. For example:

Mammal.Human.FirstName - Where Human will be null. But I don't want to perform a null check for each foreign key class. For example: Mammal.Dog.Age (now Dog could be null).

Many thanks in advance!
Kind regards,
Posted

1 solution

I think there are two options. 1) if you have access to the classes code then you might want to return a string that suggests a null, e.g. "NOTFOUND".
2) You really don't have any other option other than to do a null check.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900