6

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?

8
  • The error you posted also shows an HttpUnhandledException. Where does that come from, and what does --> mean? Commented Oct 16, 2012 at 0:51
  • 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. Commented Oct 16, 2012 at 0:55
  • @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. Commented Oct 16, 2012 at 0:56
  • @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. Commented Oct 16, 2012 at 0:59
  • 1
    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. Commented Oct 16, 2012 at 1:05

5 Answers 5

4

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.

Sign up to request clarification or add additional context in comments.

6 Comments

This is kind of what I was afraid of (and suspected) :) I'm guessing I'll be accepting yours as the answer when I do get to the bottom of it. +1 for now.
This is not true, what if MyClass has has public static bool operator ==(MyClass a, MyClass b) { throw new NullReferenceException(); }
It's not possible to get a NullReferenceException with that stack trace. The call to operator== would have been on the stack.
Accepted as answer; I rebuilt and redeployed the binary and the exception is now as excepted - obviously something went wrong on the last deployment of this site.
The moral of the story: If you get a NullReferenceException when executing your code, and the source code indicates that it is not possible to get a NullReferenceException in the line indicated in the stack trace, then it's because your source code does not match the binary code you are executing.
|
2

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

2 Comments

Not a bad thought +1; but I did check for anything like this as mentioned in the question.
Yea, and I had a == override and totally forgot about it!
1

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.

2 Comments

Yeah but the OP is asking about the NullReferenceException that exception wraps.
Is he passing a null value into myMethod()? If he is, the if statement will evaluate true, and throw that exception. If he isn't, then the if statement will evaluate false, and not throw that exception. Is he certain he isn't passing a null reference to the method and getting the exception as expected?
1

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

Comments

-3

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

2 Comments

-1: How would thing == null cause a NullReferenceException?
It did on my box. I copied and pasted the code. It jumped directly to the the NullReferenceException. Maybe optimization moved the code to one line. Try it and see if you experience the same behavior. (VS 2012). Wait...I'll update my post.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.