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!

raz3r
  • 3,071
  • 8
  • 44
  • 66
  • 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`. – Matt Mitchell Dec 20 '10 at 08:30

3 Answers3

11

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.

Gabe
  • 84,912
  • 12
  • 139
  • 238
  • 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 Skeet Dec 20 '10 at 08:33
  • 3
    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. – Gabe Dec 20 '10 at 08:35
  • 1
    Whoops, you're absolutely right. Hadn't seen that. +1, and deleting my answer :) – Jon Skeet Dec 20 '10 at 08:37
  • That worked! I should have thinked about that, thank you guys so much, all of you. Sorry if I am noob :( – raz3r Dec 20 '10 at 08:37
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>())
{

}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 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() but again, NullReferenceException :( – raz3r Dec 20 '10 at 08:28
  • @raz3r, `ArrayList`??? Please don't. Use strongly typed collections. Also are you sure that it is not the `myField` variable that is null? – Darin Dimitrov Dec 20 '10 at 08:29
  • 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 Dec 20 '10 at 08:34
  • 4
    @raz3r: You really *should* explain why you can't "handle this outside the foreach" - it's a natural answer, and a very *unnatural* restriction. – Jon Skeet Dec 20 '10 at 08:35
  • It regards the structure of the program. I am working on a software that creates Linux Firewalls and those foreach are supposed to build multiobject-rules. It is really hard to explain why I must do it that way, trust me. – raz3r Dec 20 '10 at 08:42
  • 2
    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** – jgauffin Dec 20 '10 at 08:55
  • Perhaps because null means something for me? With this way I used only few lines of code and I reached my objective. Basically I am looping through an ArrayList of MyField. MyField has another ArrayList inside that means I am a Multiple Object. I need to loop through all MyField BUT if one of them's got an ArrayList that is not null I have to loop through him too because it means that the object has been changed and now what does really matter is the ArrayList inside and not MyField. I don't know how to explain this in a few words, it will require me to explain the purpose of all the project. – raz3r Dec 20 '10 at 09:12
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.

TalentTuner
  • 17,262
  • 5
  • 38
  • 63