Click here to Skip to main content
Click here to Skip to main content

Tagged as

Walk 'object tree' with easy null handling

, 19 Dec 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
A simple 'NullHelper' class helps dealing with walking an object tree without the need for null checking at each step. The solution is efficient because it does not use reflection.
This is useful when:
1) You need to get a property-of-a-property-of-a-property-of-a-property
2) One of the objects in the path may be null, but you don't want to check each one individually
 
Note 1: This solution is a bit raw, because any null reference exception will be ignored, even when it occurs at the end of the path.
 
Note 2: Java has some nice support for this problem, see Programming mistake No. 1: http://infoworld.com/d/developer-world/12-programming-mistakes-avoid-292[^]
 
using System;
 
namespace NullHelperTest
{
    /// <summary>
    /// Helps in dealing with null.
    /// </summary>
    public static class NullHelper
    {
        /// <summary>
        /// Delegate for getting the value.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public delegate T GetDelegate<T>()
            where T : class;
 
        /// <summary>
        /// Executes delegate and returns null if null reference exception was thrown.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="getDelegate"></param>
        /// <returns></returns>
        public static T Get<T>(GetDelegate<T> getDelegate)
            where T : class
        {
            T result = null;
            try
            {
                // Execute delegate to retrieve value
                result = getDelegate();
            }
            catch (NullReferenceException)
            {
                // Ignore
            }
            return result;
        }
    }
 
    public class Level1
    {
        public Level2 Level2 = null;
    }
    public class Level2
    {
        public Level3 Level3 = null;
        public Level3 GetLevel3()
        {
            return this.Level3;
        }
    }
    public class Level3
    {
    }
    class Program
    {
        static void ClassicApproach(Level1 level1)
        {
            if (level1 != null)
            {
                Level2 level2 = level1.Level2;
                if (level2 != null)
                {
                    Level3 level3 = level2.Level3;
                    if (level3 != null)
                    {
                        // use level 3
                    }
                    else
                    {
                        Console.WriteLine("From classic: level 3 is null");
                    }
                }
                else
                {
                    Console.WriteLine("From classic: level 2 is null");
                }
            }
            else
            {
                Console.WriteLine("From classic: level 1 is null");
            }
        }
        static void NewApproach(Level1 level1)
        {
            Level3 level3 = NullHelper.Get<Level3>(() => level1.Level2.Level3);
            if (level3 != null)
            {
                // use level 3
            }
            else
            {
                Console.WriteLine("From new: level 3 is null");
            }
        }
        static void Main(string[] args)
        {
            Level1 level1 = new Level1();
            ClassicApproach(level1);
            NewApproach(level1);
        }
    }
}

License

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

Share

About the Author

Martijn Boeker
Software Developer (Senior) Phi International
Canada Canada
Grew up in Amsterdam, now living in downtown Vancouver. There are definitely more mountains here.
 
My first internship was with the first company in the Netherlands to teach C++ (www.datasim.nl). During this internship I got to know Object Oriented Design, which kept my interest until this day. In the mean time, I have worked for different companies in the Netherlands and Canada. I have done most of my recent work in C#, developing Database/Web/Desktop applications.
 
I am currently working as a freelance Software Developer for PHI International in Amsterdam.
 
The CodeProject rocks!

Comments and Discussions

 
GeneralI agree. It will be most useful when you only expect null if... PinmemberMartijn Boeker16-Dec-10 7:27 
GeneralThis will kill you in a debugger. Usually you want to catch ... PinmemberQwertie15-Dec-10 17:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141223.1 | Last Updated 19 Dec 2010
Article Copyright 2010 by Martijn Boeker
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid