1

I have data in which i'm getting

NullReferenceException : Object reference not set to an instance of an object

I know it has to do with an intentionally created attempt at making a null in a list. I'm using linqpad to try and mimic my code

List<string> list = new List<string>();
list.Add(null);
list.Add("scorp\\jack");
list.Add("you");

var x = "jack";
var admin = 0;
foreach (var y in list) // Loop through List with foreach.
{
    //Console.WriteLine(prime);
    if(y.Contains(x))
    {
        admin = 1;
    }


}

Console.WriteLine(admin);

As soon as it runs over

 if(y.Contains(x)) 

Then it cannot handle the null and I wonder what graceful way of handling it would be?

2 Answers2

1
List<string> list = new List<string>();
list.Add(null);
list.Add("scorp\\jack");
list.Add("you");

var x = "jack";
var admin = 0;

// Add the Where extension method to filter out null values.
foreach (var y in list.Where(xx => xx != null)) // Loop through List with foreach.
{
    //Console.WriteLine(prime);
    if(y.Contains(x))
    {
        admin = 1;
    }


}

Console.WriteLine(admin);

Conversely, you could leave the loop alone and change the if to:

    if(y != null && y.Contains(x))
The Sharp Ninja
  • 1,041
  • 9
  • 18
  • I like this solution, because when everyone suggests to check nullness right here and right now, this solution prevents null values in foreach body altogether. Still, it's just one of the alternatives and it can be wrong solution if we would need to handle null values inside loop. – CodingFeles Dec 16 '15 at 07:38
  • @CodingFeles meh... adding LINQ just to check `null`? that is a lot of unnecessary overhead and code confusion for that aim (compared to the nice clean value-typed allocation-free custom list iterator that `foreach` will use on `List` otherwise)... – Marc Gravell Dec 16 '15 at 07:47
  • @MarcGravell - Code confusion? It's about as clear and self documenting as it gets. And the overhead is minimal. You've got to check for null either way and changing scope, THEN checking for null has to be more expensive. – The Sharp Ninja Dec 16 '15 at 07:50
  • the `.Where` bundled onto the end is not exactly the cleanest. As for overhead - how familiar are you with what these two things will be doing under the covers? Iterating a `List` directly is *very* efficient (it has a custom value-typed iterator - it doesn't use `IEnumerable`/`IEnumerator`) - and an in-place `null` check is just a `br_true` / `br_false` - incredibly cheap. To do it with linq requires a *reference-type* iterator and enumerator allocation, a delegate allocation (although reused / cached), and multiple virtual calls and a delegate invoke per iteration step. – Marc Gravell Dec 16 '15 at 07:56
  • short version: " and changing scope, THEN checking for null has to be more expensive. " is an incorrect assertion – Marc Gravell Dec 16 '15 at 07:57
  • @MarcGravell, I agree with overhead, but this is the only answer that highlited general preventing null values processing, not just immidiate local check. Thank you for info about `List` custom iterator. Never heard about it, will research this now :) – CodingFeles Dec 16 '15 at 07:59
  • 1
    @CodingFeles to save you looking: `List.GetEnumerator()` returns a `List.Enumerator`; the compiler will prefer this, ignoring `IEnumerable` etc – Marc Gravell Dec 16 '15 at 08:01
0

You'll have to add the null condition to your if statement:

if( y != null && y.Contains(x) )  {
  // ...
}
domdomcodecode
  • 2,355
  • 4
  • 19
  • 27