R#5.1 Possible 'System.NullReference Exception' why?

I have a simple Windows Form with a TextBox named textBox1. In the class I have the following code:

    public partial class Form1 : Form     {         public Form1()         {             InitializeComponent();
            textBox1.Parent = this; // No problem here             SetParent(null, this);             SetTextBoxParent(this);         }         private void SetParent(Control child, Control parent)         {             if (parent == null) return;             if (parent == child)                 child.Parent = this;             else                 child.Parent = parent; // No problem here         }         private void SetTextBoxParent(Control parent)         {             if (parent == null) return;             if (parent == textBox1)                 textBox1.Parent = this;             else                 textBox1.Parent = parent; // Possible System.NullReference Exception         }     }

Resharper is showing a blue curly line under textBox1 in the last line of code (textBox1.Parent = parent). Its tooltip says "Possible 'System.NullReference Exception'" (see image).
R#5280724.JPG
It is true that I don't check if textBox1 isn't null. But in the Form1() constructor I don't get this warning. And what seems worse is that I also don't get the warning in the SetParent method. That method accepts a child parameter which I don't check for null, but no warning there. I even call the method from the constructor using null so the program will generate a NullReferenceException there!

I suppose the reason why the warning appears in SetTextBoxParent is that the method could be called before textBox1 gets initiated. But I don't get why the same warning doesn't appear in the SetParent method.

So my question is whether this is a bug or my lack of understanding?

Update: Even if I place the following line at the start of SetTextBoxParent it doesn't show the warning on that line!

            textBox1.Parent = parent;

Only the same line in the else statement shows the warning. If I change the expression in the if statement it's the textBox1 in the if statement that gets the warning.

Added Update section and screenshot

11 comments
Comment actions Permalink

Hello Dick,

I've checked the sample and I get a warning on both 'child.Parent = parent;'
and 'textBox1.Parent = parent;' lines. The reason for this warning is the
following: first you explicitly compare 'parent' with 'null', so ReSharper
explicitly marks 'parent' as something that is not null, then you explicitly
compare 'textBlock1' with 'parent' (which is something that is not null),
so ReSharper considers this condition as 'textBlock1 != null' and therefore
textBlock1 is considered to be possibly null in the else branch. Thank you!

Andrey Serebryansky
Senior Support Engineer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"

I have a simple Windows Form with a TextBox named textBox1. In the
class I have the following code:

 Form1 : Form
> {
> public Form1()
> {
> InitializeComponent();
> textBox1.Parent = this; // No problem here
> SetParent(null, this);
> SetTextBoxParent(this);
> }
> private void SetParent(Control child, Control parent)
> {
> if (parent == null) return;
> if (parent == child)
> child.Parent = this;
> else
> child.Parent = parent; // No problem here
> }
> private void SetTextBoxParent(Control parent)
> {
> if (parent == null) return;
> if (parent == textBox1)
> textBox1.Parent = this;
> else
> textBox1.Parent = parent; // Possible
> System.NullReference Exception
> }
> }]]>

Resharper is showing a blue curly line under textBox1 in the last line
of code (textBox1.Parent = parent). Its tooltip says "Possible
'System.NullReference Exception'". It is true that I don't check if
textBox1 isn't null. But in the Form1() constructor I don't get this
warning. And what seems worse is that I also don't get the warning in
the SetParent method. That method accepts a child parameter which I
don't check for null, but no warning there. I even call the method
from the constructor using null so the program will generate a
NullReferenceException there!
I suppose the reason why the warning appears in SetTextBoxParent is
that the method could be called before textBox1 gets initiated. But
I don't get why the same warning doesn't appear in the SetParent
method.

So my question is whether this is a bug or my lack of understanding?

---
Original message URL:
http://devnet.jetbrains.net/message/5280724#5280724



0
Comment actions Permalink

Hi Andrey,

You explanation makes sense. Now I understand the meaning of the warning.

It's interesting though that you get the warning on both lines while i only get it on the second line. The question then becomes why I don't get the warning on the line

child.Parent = parent;


Maybe the version of Resharper matters? I'm using build 5.1.1753.4.

Here's a bigger screenshot, showing that on my system the warning only appears on the second line.
R#5280724B.JPG
Also, the Actions List on the child.Parent in the else offers to "Check if 'child' is not null". The child.Parent in the if doesn't have a Actions List (which makes sense, as child can't be null if it equals parent and parent has just been checked for null).
In the SetTextBoxParent method, both textBox1.Parent lines offer to "Check if 'textBox1' is not null". For the line inside if that doesn't make sense (if parent is not null and parent equals textBox1, textBox1 can't be null).

What could cause this?

0
Comment actions Permalink

Hello Dick,

I've checked this with 5.1.1753.4 and I also get the warning on both lines.
Could you please attach the whole solution here? Thank you!

Andrey Serebryansky
Senior Support Engineer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"

Hi Andrey,

You explanation makes sense. Now I understand the meaning of the
warning.

It's interesting though that you get the warning on both lines while i
only get it on the second line. The question then becomes why I don't
get the warning on the line

 parent;]]>


Maybe the version of Resharper matters? I'm using build 5.1.1753.4.

Here's a bigger screenshot, showing that on my system the warning only
appears on the second line.
Image:R#5280724B.JPG
What could cause this?
---
Original message URL:
http://devnet.jetbrains.net/message/5280870#5280870



0
Comment actions Permalink

I've attached the complete solution.

Just to be sure, I've also included an export of my Code Style Settings.

I'm using Visual Studio 9.0.30729.1 SP on Windows XP SP3

This are the Inspection Results:

6 issues found   Common Practices and Code Improvements (3 issues)     <PossibleSystemNullReferenceException>\Program.cs (2 issues)       Using directive is not required by the code and can be safely removed       Using directive is not required by the code and can be safely removed     <PossibleSystemNullReferenceException>\Properties\AssemblyInfo.cs (1 issue)       Using directive is not required by the code and can be safely removed   Language Usage Opportunities (2 issues)     <PossibleSystemNullReferenceException>\Form1.cs (2 issues)       Convert to '?:' expression       Convert to '?:' expression   Potential Code Quality Issues (1 issue)     <PossibleSystemNullReferenceException>\Form1.cs (1 issue)       Possible 'System.NullReferenceException'



Attachment(s):
ResharperCodeStyleSettings.xml
PossibleSystemNullReferenceException.zip
0
Comment actions Permalink

Hello Dick,

Weird enough, the sample solution you've attached gives 'Possible NullReferenceException'
on both lines for me. Could you please also attach the %APPDATA%\JetBrains\ReSharper\\UserSettings.xml
file and a screenshot of the text editor with Form1.cs open with both lines
visible? Thank you!

Andrey Serebryansky
Senior Support Engineer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"

I've attached the complete solution.

Just to be sure, I've also included an export of my Code Style
Settings.

I'm using Visual Studio 9.0.30729.1 SP on Windows XP SP3

---
Original message URL:
http://devnet.jetbrains.net/message/5281025#5281025



0
Comment actions Permalink

Hi Andrey,

That's weird indeed.

Since I'm using a VMware Virtual Machine, I've reverted it to a snapshot taken before installing Resharper. That gives me a VM with only Visual Studio installed. From there, I installed Resharper again (using http://download-ln.jetbrains.com/resharper/ReSharperSetup.5.1.1753.4.msi). I created a new Windows Form project, added a TextBox control and copied in the code. So, no user settings are altered. But just to be sure, I've included my UserSettings.xml file. It was not in the folder you specified, but inside the folder v5.1\vs9.0. I've attached the complete contents of my %ApppData%\JetBrains folder.

I've also include the screenshot you requested, although I already did that in one of my previous posts.

It seems like the difference between your and mine situation might not be in Resharper itself, but is somehow caused by the OS or the Visual Studio installation itself? I can't imagine how it'd make a difference, but my OS is installed from a slipstreamed installation DVD containing Windows XP SP3 MUI, IE8, WMP1 and .Net Framework 1.1, 2.0 and 3.5.


It's definitely weird. If I just create a class like this:

using System.Windows.Forms; public class Class1 : Form {     private Control child;     private void SetParent(Control parent)     {         if (parent == null) return;         if (parent == child)             child.Parent = null;         else             child.Parent = parent;     } }


I do get the warning on the second assignment line.

But if I switch the expressions in the if (parent == child) comparison:

using System.Windows.Forms; public class Class1 : Form {     private Control child;     private void SetParent(Control parent)     {         if (parent == null) return;         if (child == parent)             child.Parent = null;         else             child.Parent = parent;     } }


The warning doesn't appear anymore.

If I remove the check for parent not being null, I'd expect to get the warning on both assignment lines. However, I don't get the warning at all. Am I right in expecting it on both lines?

So... it seems like the warning only appears on class fields, not on method parameters. Aside from that, it seems like the warning only appears if the variable that has been explicitly checked not to be null, appears on the left hand side of the equality operator.

Attachment(s):
PossibleSystemNullReferenceException.png
AppData_JetBrains_ReSharper.zip
0
Comment actions Permalink

I've just tried with ReShaper 5.1.2 but I still only get the warning on the line

textBox1.Parent = parent;
0
Comment actions Permalink

Today I was finally able to test this on another machine with another OS and another install of VS2008. I started working for a new client who is using VS2008 (9.0.30729.1 SP) on Windows Server 2008 SP2 (6.0.6002). I downloaded and installed the trial for ReSharper 5.1.3 (5.1.3000.12).

However, on this machine I also get the warning only on the last line!

So, it doesn't have to do with my OS settings, it must be something else. I have tried changing Regional Settings (cause both machines are set to Dutch) to English US (and then restarted VS2008) but that also didn't make any difference.

Could you post a screenshot showing that you do get the warning on both lines? Maybe somehow we are not understanding each other correctly.

0
Comment actions Permalink

Hi, Dick

It's really strange for me but I have only one highlighting. I have
tested this with both R# 5.1.3 final release and R# 6.0 EAP build. I
think we should wait for Andrey (he is on vocation now) and compare our
environment and R# settings.

Thank you!

--
Kirill Falk
.NET Team QA Lead
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"

0
Comment actions Permalink

Hi Kirill,

Is Andrey still on vacation?

0

Please sign in to leave a comment.