Sunday, March 6, 2011

Ignore NullReferenceException when reading object properties

Is there any way to direct C# to ignore NullReferenceException (or any specific exception for that matter) for a set of statements. This is useful when trying to read properties from a deserialized object that may contain many null objects in it. Having a helper method to check for null could be one way but I'm looking for something close to 'On Error Resume Next' (from VB) at a block of statement level.

EDIT:Try-Catch will skip the succeeding statements on exception

try
{
   stmt 1;// NullReferenceException here, will jump to catch - skipping stmt2 and stmt 3
   stmt 2;
   stmt 3;
}
catch (NullReferenceException) { }

For Example: I'm deserializing an XML message to an object and then try to access a property like

Message.instance[0].prop1.prop2.ID

now prop2 could be a null object (because it doesn't exists in XML Message - an optional element in XSD). right now I need to check for null for each element in the hierarchy before accessing the leaf element. i.e I've to check if instance[0], prop1, prop2 are not null, before accessing 'ID'.

Is there a better way that avoids null-checking for each element in the hierarchy?

From stackoverflow
  • In short: no. Null-check the reference before trying to use it. One useful trick here might be C# 3.0 extension methods... they allow you to appear to invoke something on a null reference without error:

    string foo = null;
    foo.Spooky();
    ...
    public static void Spooky(this string bar) {
        Console.WriteLine("boo!");
    }
    

    Other than that - perhaps some use of the conditional operator?

    string name = obj == null ? "" : obj.Name;
    
  • try
    {
       // exceptions thrown here...
    }
    catch (NullReferenceException) { }
    
    Marc Gravell : Except you'd need that for every line that might throw...
  • The ternary operator and/or ?? operator may be useful.

    Say you are trying to get the value of myItem.MyProperty.GetValue(), and MyProperty could be null, and you want to default an empty string:

    string str = myItem.MyProperty == null ? "" : myItem.MyProperty.GetValue();
    

    Or in the case where the return value of GetValue is null, but you want to default to something:

    string str = myItem.MyProperty.GetValue() ?? "<Unknown>";
    

    This can be combined to:

    string str = myItem.MyProperty == null 
        ? "" 
        : (myItem.MyProperty.GetValue()  ?? "<Unknown>");
    
  • I would go with the helper method. On Error Resume Next just leads to madness.

  • now I'm using delegate and NullReferenceException handling

    public delegate string SD();//declare before class definition
    
    string X = GetValue(() => Message.instance[0].prop1.prop2.ID); //usage
    
    //GetValue defintion
    private string GetValue(SD d){
            try
            {
                return d();
            }
            catch (NullReferenceException) {
                return "";
            }
    
        }
    

    Thanks to http://stackoverflow.com/questions/117173/c-try-catch-every-line-of-code-without-individual-try-catch-blocks for the idea

0 comments:

Post a Comment