6
votes

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?

5
The error you posted also shows an HttpUnhandledException. Where does that come from, and what does --> mean? - Chris Laplante
If you have just added this check, verify that your site uses up-to-date binaries. To avoid potential problems with overloading, there's always object.ReferenceEquals. - Anton Tykhyy
@SimpleCoder: that's just the first line of the stacktrace from Elmah - I've edited the question to include the next line too. Note line 100 in the source file is the if statement. - mutex
@AntonTykhyy: Nope, I've looked at this file in our source conotrl and the check has always been in place... which is why I'm so confused. - mutex
Rather than depending on what unhandled exceptions are thrown and looking at the messages, debug your application and verify that everything is as you claim it to be. - Jeff Mercado

5 Answers

4
votes

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.

2
votes

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);
   } 
}
1
votes

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.

0
votes

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;
}
-3
votes

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....
}