Getting a NullReferenceException in an impossible situation (when checking for zero)

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

public void MyMethod(MyClass thing) { if(thing == null) throw new ArgumentNullException("thing"); //Do other stufff.... } 

But I get stacktraces (from Elmah in the production environment) which seems to indicate that the if line (thing == null) throws a NullReferenceException. The first two lines of the stack trace look something like this:

 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 pretty simple class without operator overloads or something like that, so I'm a little fixated on throwing a NullReferenceException!

Can anyone suggest scripts that might trigger this?

EDIT: I suspect the β€œthing” may be null, but I would really expect ArgumentNullException to be a NullReferenceException - this is basically what this question is about. Maybe something that the wireframe or Elmah is changing or is reporting the exception incorrectly - or is this the only explanation that the binaries are somehow out of date?

+6
source share
5 answers

It is not possible for if (thing == null) NullReferenceException .

This means that something else is happening. The time has come to start using your imagination, combined with a firm determination to ignore the possibility that the if problem has caused the problem.

+3
source

It seems that the exception comes from something called the MyMethod chain. MyMethod () throws an exception and nothing handles it above, so any web structure you are in throws an HttpUnhandledException.

+1
source

The if statement may raise a NullReferenceException if MyClass incorrectly defines the == statement, for example

 class MyClass { int A {get;set;} public static bool operator ==(MyClass a, MyClass b) { return aA == bA; } public static bool operator !=(MyClass a, MyClass b) { return !(a == b); } } 
+1
source

I also encountered this impossible situation. This turned out to be related to using the as keyword, I have no idea why. I used 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 part, it will work. So now I have two levels of validation in my code:

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

thing is zero.

It will cause him.

[EDIT]: Here is the code I tested:

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

Source: https://habr.com/ru/post/927796/


All Articles