This Question is Possibly Answered

1 "correct" answer available (4 pts) 2 "helpful" answers available (2 pts)
3 Replies Last post: Jan 2, 2012 2:42 PM by Evgeny Pasynkov  
Martin Cook Newbie 2 posts since
Dec 22, 2011
Currently Being Moderated

Dec 22, 2011 10:31 PM

XPathNodeIterator Current

In the C# code below, R# 5.1 and R# 6.0 both report a possible NullReferenceException for iter.Current.  I don't understand how this can be.

 

According to MSDN, if iter.MoveNext() returns true "it moves the XPathNavigator object returned by the Current property to the next node in the selected node set", so I believe that the while loop can only be entered if iter.Current has been set.

 

Why then does R# report a possible NullReferenceException? Am I missing something or is this a bug?

 

 

 

string xmlTest;


XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlTest);


XPathNavigator nav = doc.CreateNavigator();


XPathNodeIterator iter = nav.Select("/valid/path");
while (iter.MoveNext())
{
      XPathNodeIterator users = iter.Current.Select("");
}


Evgeny Pasynkov JetBrains 6,085 posts since
Dec 3, 2003
Currently Being Moderated
Dec 27, 2011 8:51 PM in response to: Martin Cook
Re: XPathNodeIterator Current

Definitely, ".Current" could be null (for example, if enumerable collection contains 'null' inside). But it looks like ReSharper is too pessimistic here .

We'll fix the annotations.

Evgeny Pasynkov JetBrains 6,085 posts since
Dec 3, 2003
Currently Being Moderated
Jan 2, 2012 2:42 PM in response to: Martin Cook
Re: XPathNodeIterator Current

If you forget to check for "iter.MoveNext()", the iterator could be null

More Like This

  • Retrieving data ...