Click here to Skip to main content
13,259,194 members (48,024 online)
Rate this:
Please Sign up or sign in to vote.
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 14-Jun-12 0:03am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.171114.1 | Last Updated 14 Jun 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100