views:

1434

answers:

2

First off, according to http://msdn.microsoft.com/en-us/library/x0b5b5bc.aspx, the List.Find method is only listed as throwing ArgumentNullException. However I have the following test code which, when using Find with an anonymous delegate, throws a NullReferenceException when the object being searched for is not found.

namespace MyTestNS
{
  class MyTestClass
  {
    [TestMethod()]
    public void ArrayMatchTest()
    {
        List<A> objArray = new List<A>();
        objArray.Add(new A("1","one"));
        objArray.Add(new A("2", "two"));

        string findStr = "3";
        string foundVal;
        // Find using an anonymous delegate:
        foundVal = objArray.Find(delegate(A a) // <- System.NullReferenceException: Object reference not set to an instance of an object..
        {
            if (a.name == findStr)
                return true;
            else return false;
        }).value;
    }
  }
}

I don't understand why I'm getting a NullReferenceException instead of the Find just not finding the item and returning null. I'm 90% sure it's some subtle coding error on my part that I just haven't seen, but this has been bugging me all day, please help!

EDIT: I should mention I inherited this convoluted code form someone else, so the twisty code you see above is a somewhat simplified version of whats failing in my real code.

+18  A: 

Find is returning null. But then you are dereferencing that result. That is, you're invoking:

Find(...).value

samael
You are exactly correct! Sometimes it stinks maintaining someone else's convoluted code!
Ogre Psalm33
+3  A: 

You have a lot of stuff going on in one place - I would suggest that you simplify it a bit so that you can see exactly what is going wrong.

A simpler version of your Find invocation is this:

A a1 = objArray.Find(a => a.name == findStr);

What happens if your Predicate<A> returns false? The objArray returns you a null reference and a is assigned to it. Now it is clear to see that using a will cause a NullReferenceException.

Andrew Hare
I bumped your answer too because this is a step towards samuel's solution. I inherited this convoluted code from someone else, so I should have simplified it to start with, as you suggested.
Ogre Psalm33