mutex
mutex

Reputation: 7728

Getting NullReferenceException in impossible situation (when checking for null)

I have a very simple check right at the beginning of one of my methods as follows:

public void MyMethod(MyClass thing)
{
    if(thing == null)
        throw new ArgumentNullException("thing");

    //Do other stufff....
}

But I'm getting stacktraces (from Elmah in a production environment) which appears to indicate that the "if(thing == null)" line is throwing a NullReferenceException. The first 2 lines of the stack trace are something like:

System.Web.HttpUnhandledException: Exception of type 'System.Web.HttpUnhandledException' was thrown. ---> System.NullReferenceException: Object reference not set to an instance of an object.
   at MyLibrary.BL.AnotherClass.MyMethod(MyClass thing) in C:\Development\MyProject\trunk\MyLibrary.BL\AnotherClass.cs:line 100

MyClass is a fairly simple class with no operator overloads or anything like that, so I'm a bit stumped as to what is throwing the NullReferenceException!

Can anybody suggest scenarios that might cause this?

EDIT: I suspect "thing" might be null, but I really would expect an ArgumentNullException not a NullReferenceException - this is basically what this question is about. Is there maybe something that the framework or Elmah that is changing or mis-reporting the exception - or is the only explanation that the binaries are somehow out of date?

Upvotes: 6

Views: 3212

Answers (5)

Oran Dennison
Oran Dennison

Reputation: 3297

I also encountered this impossible situation. It turned out to be due to the use of the as keyword, I have no idea why. I was using the SharpPdf library and had a line of code like this:

var destElement = annotDict.Elements["/Dest"] as PdfName;
if (destElement == null)
{
    continue;
}

If I remove the as PdfName portion, it works. So I now have two levels of checking in my code:

var destElement = annotDict.Elements["/Dest"];

if (destElement == null)
{
    continue;
}

var destElementName = destElement as PdfName;
if (destElementName == null)
{
    continue;
}

Upvotes: 1

Steve Wellens
Steve Wellens

Reputation: 20620

thing is null.

That would cause it.

[EDIT]: Here's the code I tested with:

protected void Button3_Click(object sender, EventArgs e)
{
    MyMethod(null);
}

public void MyMethod(String thing)
{
    if (thing == null)   //  This caused the exception to be thrown.
        throw new Exception("test");

    //Do other stufff....
}

Upvotes: -3

Matthew Finlay
Matthew Finlay

Reputation: 3464

The if statement can throw a NullReferenceException if MyClass defines the == operator incorrectly e.g.

class MyClass
{
   int A {get;set;}

   public static bool operator ==(MyClass a, MyClass b)
   {
      return a.A == b.A;
   }

   public static bool operator !=(MyClass a, MyClass b)
   {
      return !(a == b);
   } 
}

Upvotes: 2

John Saunders
John Saunders

Reputation: 161773

It is impossible for if (thing == null) to throw a NullReferenceException.

This means that something else is going on. It's time to start using your imagination in conjunction with a firm resolve to ignore the possibility that the if caused a problem.

Upvotes: 4

speakingcode
speakingcode

Reputation: 1506

Looks like the exception is coming from something up the chain that calls MyMethod. MyMethod() is throwing the Exception and nothing above is handling it, so whatever web framework you're in is throwing the HttpUnhandledException.

Upvotes: 1

Related Questions