Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NullReferenceException on a conditional null check?

I'm trying to fix a bug that I cannot reproduce (yipeee!). I have the stack trace that was copied by the user that originally discovered the issue, and it shows the code throwing a null reference exception (which is unhandled) on a line that is checking the object for null..like this:

private void someFunction()
{
    radioButton1.CheckedChanged -= checkedChangedEventHandler
    radioButton2.CheckedChanged -= checkedChangedEventHandler

    if (someObject != null)  // throws NullReferenceException...allegedly
    {
         if (someObject.Property == something)
         {
            // set properties on some UI components
         }
    }
}

What kind of conditions could cause this?

UPDATE

Added some more code. SomeFunction method gets called by the checkedChanged event handlers.

UPDATE 2

The stack trace must be wrong as several of you have suggested. There are no operator overloads, and the method only references four objects that are not UI components (labels and radio buttons), and all of those objects are assigned only once on initialization, and are referenced multiple times before ever getting to this code so any null references would have been caught way before this. I'll have to look more closely at the calling event handler function.

Here is a thread explaining stack traces with wrong line numbers:

Wrong line number on stack trace

like image 855
alexD Avatar asked Jul 22 '26 19:07

alexD


1 Answers

someObject has overloaded != operator?

http://msdn.microsoft.com/en-us/library/8edha89s(v=vs.71).aspx

like image 114
Daniel Mošmondor Avatar answered Jul 25 '26 07:07

Daniel Mošmondor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!