2

I have this line of code that can throw null exceptions.

singleAddress.FullAddress = cc.MailingAddressStreet1.ToString() + " " +
   cc.MailingAddressCity.ToString() + " " +
   cc.MailingAddressState.ToString() + " " +
   cc.MailingAddressZip.ToString() + " " +
   cc.MailingAddressCountry.ToString();

I know that I can fix it by adding if statements to check if it is null. But is there a better recommended way to do it?

I just want to learn how to handle such exceptions better (and not have to write more code than I need to). Thanks in advance.

5
  • 11
    Well what do you want the result to be if any of these values is null? And what's the type of each value? (If it's already string, why are you calling ToString?) Commented Mar 5, 2012 at 17:48
  • Nothing. If someone did not provide a Country then the string should just not add anything. I didn't think about that When I wrote the question. Thanks for pointing that out. Commented Mar 5, 2012 at 18:01
  • 2
    And my point about the needless calls to ToString? Commented Mar 5, 2012 at 18:02
  • no idea why I put the toString there. They were all string values. Sorry about the confusion. Commented Mar 5, 2012 at 18:18
  • Actually I realized that the .toString() was the reason why the exception was thrown out. I went back and removed all of them and the code worked fine. Now I know why you were asking about the toString :). Thanks @JonSkeet! Commented Mar 5, 2012 at 18:56

4 Answers 4

10

You can use the String.Join Method:

if (cc != null)
{
    singleAddress.FullAddress = string.Join(" ",
        cc.MailingAddressStreet1,
        cc.MailingAddressCity,
        cc.MailingAddressState,
        cc.MailingAddressZip,
        cc.MailingAddressCountry);
}

The String.Join Method takes a variable number of object arguments and calls the Object.ToString Method on each argument that is not null.

Sign up to request clarification or add additional context in comments.

Comments

3

The String.Join method seems like a good way to go, but don't forget the null coalescing operator, e.g.

var s = (cc.MailingAddressStreet1 ?? string.Empty) + ...

I'm assuming that cc.MailingAddressStreet1 is already a string though.

This gives you the option of using an alternative string when the string is null, e.g.

var s = (cc.MailingAddressStreet1 ?? "(n/a)") + ...

And don't forget the brackets :)

Comments

3

I'd probably just use string.Format:

singleAddress.FullAddress = string.Format("{0} {1} {2} {3} {4}",
    cc.MailingAddressStreet1, cc.MailingAddressCity, cc.MailingAddressState,
    cc.MailingAddressZip, cc.MailingAddressCountry);

The NullReferenceExceptions were due to calling ToString - which will happen anyway for non-null values (even in your original code), and is pointless if the property types are already string...

Comments

1

String.Concat should work

http://msdn.microsoft.com/en-us/library/system.string.concat.aspx

singleAddress.FullAddress = String.Concat(cc.MailingAddressStreet1.ToString(), 
                                        " ", cc.MailingAddressCity.ToString(), 
                                        " ", cc.MailingAddressState.ToString(),
                                        " ", cc.MailingAddressZip.ToString(),
                                        " ", cc.MailingAddressCountry.ToString());

2 Comments

I still got the nullReferenceException with this one because MailingAddressCountry was null. But thanks for telling me about Concat.
Correction: It works if I remove the .toString. Sorry about that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.