8
foreach (string s in myField.getChilds()) {
    if (s == null)
        //handle null
    else
        //handle normal value 
}

When I run my program i get a NullReferenceException because getChilds may return null. How can I make my program to continue anyway and handle the exception? I can't handle it outside of the foreach, can't explain why because it will take too much time (and I am sure you guys are busy :P). Any ideas?

I already tryed that way:

foreach (string s in myField.getChilds() ?? new ArrayList(1)) {
        if (s == null)
            //handle null
        else
            //handle normal value 
    }

But it does not work, program just jump at the end of the foreach but I want it to enter the foreach instead!

1
  • I don't understand your comment regarding the null coalescing operator: "it does not work, program just jump at the end of the foreach but I want it to enter the foreach instead!". It's possible because you are not giving a string that it is just jumping. You have to give it a value it can assign to s. Commented Dec 20, 2010 at 8:30

3 Answers 3

10

One way to do this (though not the best way) is:

foreach (string s in myField.getChilds() ?? new string[] { null })

or

foreach (string s in myField.getChilds() ?? new ArrayList { null })

The reason new ArrayList(1) doesn't work is that it creates a list that has the capacity to hold 1 element, but is still empty. However new string[] { null } creates a string array with a single element which is just null, which is what you appear to want.

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

4 Comments

I see no indication that the OP wants to actually go into the loop if the return value is null... I have assumed he wants to treat null in the same way as an empty collection.
Jon: When OP says "program just jump at the end of the foreach but I want it to enter the foreach instead!" I take that as an indication he wants a null collection to be treated as a one-element collection containing null.
Whoops, you're absolutely right. Hadn't seen that. +1, and deleting my answer :)
That worked! I should have thinked about that, thank you guys so much, all of you. Sorry if I am noob :(
3
var children = myField.getChilds();
if (children == null)
{
    // Handle the null case
}
else
{
    foreach (string s in children)
    {

    }
}

or simply use the null coalescing operator:

foreach (string s in myField.getChilds() ?? Enumerable.Empty<string>())
{

}

7 Comments

As I said I can't handle it outside, it must be done in the foreach. Otherwise I would have done it in that way :P I am noob but not stupid LOL. Anyway I tried your suggestion but I had to add a cast since getChilds() returns an ArrayList. So i modified it like that: string s in (string[])source.getChilds().ToArray() ?? Enumerable.Empty<string>() but again, NullReferenceException :(
@raz3r, ArrayList??? Please don't. Use strongly typed collections. Also are you sure that it is not the myField variable that is null?
Looks like I am an idiot instead because I try to call the ToArray() method on a null object. Ofcourse I get NullReferenceException anyway xD Beside that I can give it a try with a collection but then if I implement an Interface I must override all Count, GetEnumerator etc. Am I wrong? What type do you suggest? myField is absolutely not null because I use it before and I have debugged the application don't worry.
@raz3r: You really should explain why you can't "handle this outside the foreach" - it's a natural answer, and a very unnatural restriction.
It does not make any sense that you should handle stuff inside a loop if the collection you are looping is empty or null. There are nothing to process
|
2

if it is the myField.getChilds() which may contain null

than

foreach (string s in myField.getChilds()) {
if (string.IsNullOrEmpty(s))
    //handle null
else
    //handle normal value 

}

this way ,you can handel null or empty strings.

Comments

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.