LINQ VS the NULL Monster

The Problem

I was working on a task, something that seemed just way too simple.  I needed to filter a List<T> on one of the values in the objects in the List.  I put my LINQ in place and started the tests.  This change needed to happen in different areas, but it was simple reuse of code and not a big deal and so was really only two places.  The tests returned the error about an object not set to an instance of an object.  I really don’t like that error.

Tracking the Monster

Naturally I set some break points in my new code and selected a test to debug.  Imagine my surprise when none of my break points were hit and the test just sat there supposedly running.  Normally this works fine, but for some reason it just wasn’t happening today.  I did not want to waste too much time and I knew roughly where the error had to be happening.

A good friend of mine had suggested using LINQPad for more than just playing around with LINQ.  I had not really used LINQPad for a while so I pulled it down and noticed that it was so much more than just straight LINQ queries.  My friend was right, and so I used LINQPad as my tracking hound.  In my case I picked the “Language” of “C# Program” and put together something that was a simplistic version of what I was trying to accomplish.  Then I commenced to running it using different values to see what produced my problem.

void Main()
{
 List<SheldonStuff> myList = new List<SheldonStuff>();
 myList.Add(new SheldonStuff{ Id = 1, Name = "a", Quantity = 3});
 myList.Add(new SheldonStuff{ Id = 2, Name = "b", Quantity = 2});
 myList.Add(new SheldonStuff{ Id = 3, Quantity = 1});
 //myList = null;
 
 List<SheldonStuff> justA = GetAllA(myList);
 
 if(justA != null)
 {
 Console.WriteLine(justA.Count());
 }
 else
 {
 Console.WriteLine("It was null");
 }
}

// Define other methods and classes here
public List<SheldonStuff> GetAllA(List sheldonStuff)
{
 if(sheldonStuff != null && sheldonStuff.Count() > 0)
 {
 List<SheldonStuff> justA = sheldonStuff.Where(s => s.Name != null).Where(s => s.Name.ToUpper() == "A").ToList();
 return justA;
 }
 return null;
}

public class SheldonStuff
{
 public int Id;
 public string Name;
 public int Quantity;
}

Gotcha!

I found the monster.  Apparently a field that I was not aware could be null, was null.  It also just happened to be the field I needed to use as my filter.  The fix was simple enough, you can see it in the code from my LINQPad above, I just needed to add a where clause for the field not to be null.

Run my tests again, all of them pass, and everything is right again.  A simple addition to my LINQ took care of that NULL monster.

Advertisements

About SheldonS

Web developer for over 15 years mainly with Microsoft technologies from classic ASP to .NET 4. Husband, father, and aspiring amateur photographer.

Posted on August 3, 2015, in Bits, LINQ, Troubleshooting and tagged . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: