Link to home
Start Free TrialLog in
Avatar of angus_young_acdc
angus_young_acdcFlag for United Kingdom of Great Britain and Northern Ireland

asked on

What to do about NullReferenceException?

Hey everyone,

I'm reading an XML document, and assigning the attribute values to various textboxes.  This works perfectly fine so long as the attributes are there, if they aren't I get a NullReference exception.  

As my code is like the following:
 txtPostalName.Text = nodeCorAdd.Attributes.GetNamedItem("postalname").Value;

What can I do if the attribute isn't in the XML document?  I really don't want to have go:
if(nodeCorAdd.Attributes.GetNamedItem("postalname").Value != null) for each attribute.  
Avatar of William Domenz
William Domenz
Flag of United States of America image

if you perform a loop for all your items and test for null - you can set them to string.empty if they are null, then when populating your textboxes it will not bomb on yu
you could write a routine to convert to NonNullableString like this:
private string ToNonNullableString(object value)
{
    string result = string.empty;
    if (value != null)
    {
        result = value.ToString();
    }
    return result;
}
 
// Then call it like this:
txtPostalName.Text = ToNonNullableString(nodeCorAdd.Attributes.GetNamedItem("postalname").Value);

Open in new window

Actually, I think your example check is checking the value of the item for null, but if the attribute doesn't exist, won't THAT check fail because the object reference is NULL, thereby preventing checking its value. I assume that's how GetNamedItem works ... by returning a null if it's not found.

Anyway, why not create a small wrapper function that does both checks. Then you just call the wrapper, once for each field.

Off the top of my head, I don't know the Class names returned by these, so I'll use "var" as a placeholder...

MyWrapperToSetFieldValue(txtPostalName, "postalname");

private void MyWrapperToSetFieldValue(Control ctl, string fieldName)
{
    var node = nodeCorAdd.Attributes.GetNamedItem(fieldName);
    if (var != null && var.Value != null)
    {
        ctl.Text = var.Value;
    }
    else {
       ctl.Text = string.empty; // can also use TextBox as the type, if they're all text boxes. and make wrapper classes for each type (overloading) or determnine type of control in here.
    }
}
Well, which part is returning null--GetNamedItem, or the Value property of the returned item? Also, to clean up your code, you can use the "Coalesce" operator, written as "??". It works like this:

System.String someString = stringValuePossiblyNull ?? "Fallback if the value to the left is null";

The compiler, in this case, will check the value of "stringValuePossiblyNull"; if it is null, it will assign the vaule to the right of the coalesce operator. If it is not null, it will assign the value as usual.

Another option is to use an XML schema in which you require certain attributes. There are quite a few ways of going about your problem, but really, I hate to say it, but you're being lazy here. There's no reason you can't check for something as necessary as this--not everything can be just seamlessly built in and magically done FOR you.

Anyways, a more specific solution is provided in the following code snippet. If my thoughts are correct, it will still throw a NullReferenceException because I think you're checking the wrong value for "null", but we'll see :)

Hope it helps,
Nate
txtPostalName.Text = nodeCorAdd.Attributes.GetNamedItem("postalname").Value ?? System.String.Empty;
 
// If this doesn't work, and you're worried about too many lines, of code, you could use the following,
// assuming the returned item is null (which I'm thinking it is)
 
txtPostalName.Text = (nodeCorAdd.Attributes.GetNamedItem("postalname") != null) ?nodeCorAdd.Attributes.GetNamedItem("postalname").Value ?? System.String.Empty : System.String.Empty;
 
// This performs a check for both fields that may be null.

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of angus_young_acdc
angus_young_acdc
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial