6

ReSharper notifies me about a possible System.NullReferenceException for the following code:

 IEnumerator<IEdgeData> edgeEnumerator = edgeData.GetEnumerator();
 while (edgeEnumerator.MoveNext())
 {
    ConvId fromConvId = edgeEnumerator.Current.From;
    ...
 }

Specifically, it underlines:

edgeEnumerator.Current

I cannot figure out under which circumstances the exception could occur. My understanding is that the while-loops inner statements will only be executed if MoveNext() can set the enumerator on the next element.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Norbert
  • 4,239
  • 7
  • 37
  • 59

1 Answers1

7

The next element could actually be null. For example, the following code:

new List<SampleClass> { null, null, null }

will still give you an enumerator for each element, but the element itself is null.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Tejs
  • 40,736
  • 10
  • 68
  • 86