Possible NullReferenceException on XmlDocument.DocumentElement, really??

Seems like the possible null reference exception checks are a little too aggressive in 4.0. This code snippet gives a warning on the third line, for possible null reference exception on "doc.DocumentElement".

var doc = new XmlDocument();
doc.LoadXml("]]>");
Console.WriteLine(doc.DocumentElement.NamespaceURI);

This code can not produce a null reference exception as warned. While DocumentElement can technically raise a null reference exception, mostly it's referenced after one has loaded a document.

If the null reference exception warning can't be taken off from this property by default, then it'd be very nice if there was a way to turn off this warning for this property only. Not by pragma (nice, but too ugly), but a more specific option, so instead of

"Change severity of warning 'Possible System.NullReferenceException'."

it'd be better to have the option

"Change severity of warning 'Possible System.NullReferenceException' for 'System.Xml.XmlDocument::DocumentElement;."

Thanks,

Sam

5 comments
Comment actions Permalink

I really like the idea of changing severity of warning on specific properties. I have the same issue with the following and it would be nice if I could disable the warning for HttpContext.Current - since I know that there will always be a current context in my specific case.

fileName = HttpContext.Current == null ? "" : HttpContext.Current.Request.MapPath(fileName);

0
Comment actions Permalink

Well this is just the point of the suggestions, you have a chance that there could be a null reference, so just check it. For XmlDocument what if you ever forget to call Load or LoadXml. And about the HttpContext.Current, (and properties in general), there is always the race condition of thread issues with properties, same as the recommended way for first storing and event before checking it for null.

HttpContext current = HttpContext.Current;
fileName = current == null ? "" : current.Request.MapPath(fileName);

0
Comment actions Permalink

The problem with the suggestions is there's no clean way to turn them off for one line (the pragma thing is very ugly) so it's best to separate out the warnings into as many unique warnings as possible so users can choose which to show and which not to. Showing warnings, even if everything is fine, hampers productivity 'cause every time someone opens the file they should check the warnings to make sure it's ok.

And HttpContext.Current is thread local, not truly static, so you don't need to worry about race conditions like you've done.

The Possible NullReferenceException warning was pretty good in previous versions of ReSharper but now has been pushed overboard so that it crops up so often that I'll probably have to turn it off completely which means loss in functionality from a previous version.

My $0.02.

Sam

0
Comment actions Permalink

We are going to improve our NRE-analysis in a nearest future

--
Eugene Pasynkov
Developer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"
"Samuel Neff" <no_replay@jetbrains.com> wrote in message
news:20627786.1204037878624.JavaMail.itn@is.intellij.net...

The problem with the suggestions is there's no clean way to turn them off
for one line (the pragma thing is very ugly) so it's best to separate out
the warnings into as many unique warnings as possible so users can choose
which to show and which not to. Showing warnings, even if everything is
fine, hampers productivity 'cause every time someone opens the file they
should check the warnings to make sure it's ok.

>

And HttpContext.Current is thread local, not truly static, so you don't
need to worry about race conditions like you've done.

>

The Possible NullReferenceException warning was pretty good in previous
versions of ReSharper but now has been pushed overboard so that it crops
up so often that I'll probably have to turn it off completely which means
loss in functionality from a previous version.

>

My $0.02.

>

Sam



0
Comment actions Permalink

I agree with Samuel, I need to have that green light / box at the top, and I need it green not yellow or green with annotations. You guys need to pay close attention to how much you warn and even how fast you do it. For example if I'm typing a method and I haven't finished with the definition and Resharper colors the whole page in red it interrupts my flow. Rather than focusing on the new method I'm writing I have to filter out the temporary errors or check on why Reshaper thinks something is broken. It's a tough balance to achieve, and it's always good to keep that in the back of your mind. Thanks.

0

Please sign in to leave a comment.