Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
See more: C#4.0 Framework entity
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 13-Jun-12 23:03pm

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
0 OriginalGriff 7,280
1 Sergey Alexandrovich Kryukov 6,059
2 Maciej Los 3,579
3 Peter Leow 3,383
4 CHill60 2,522

Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 14 Jun 2012
Copyright © CodeProject, 1999-2014
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