stanfordnlp/CoreNLP · error · NoSuchElementException

Graph edge iterator exhausted.

Error message

Graph edge iterator exhausted.

What it means

A NoSuchElementException guard in DirectedMultiGraph's edge iterator: next() was called after hasNext() returned false, i.e. the consumer iterated past the last edge (forward or reverse view) of the graph.

Solutions

  1. Use a for-each loop or while(iterator.hasNext()) instead of manual next() calls
  2. Get a fresh iterator from the graph instead of reusing an exhausted one
  3. Guard each next() call with hasNext()

Example fix

// before
Iterator<E> it = graph.edgeIterator();
E e1 = it.next(); E e2 = it.next(); // may exhaust
// after
for (E e : graph.getEdgeIterable()) { process(e); }
Defensive patterns

Strategy: type-guard

Validate before calling

if (iterator.hasNext()) { E e = iterator.next(); }

Type guard

boolean available = it.hasNext(); // gate every next() call on this

Try / catch

try { e = it.next(); } catch (NoSuchElementException e2) { e = null; /* iterator exhausted */ }

Prevention

When it happens

Trigger: Calling next() on the graph's edge iterator after hasNext() returns false, e.g. looping past the end or calling next() an extra time in a manual iterator loop.

Common situations: Manual iterator loops with an off-by-one; calling next() once more to 'consume' a last element; reusing an exhausted iterator instead of getting a fresh one.

Related errors


AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10). Data as JSON: /api/errors/d80c2b6d8c379716. Report an issue: GitHub.

Appendix: source

Thrown at src/edu/stanford/nlp/graph/DirectedMultiGraph.java:567

      currentSource = startVertex;
      Map<V, List<E>> neighbors = source.get(startVertex);
      if (neighbors != null) {
        vertexIterator = null;
        connectionIterator = neighbors.entrySet().iterator();
      }
      this.reverseEdges = reverseEdges;
    }

    @Override
    public boolean hasNext() {
      primeIterator();
      return hasNext;
    }

    @Override
    public E next() {
      if (!hasNext()) {
        throw new NoSuchElementException("Graph edge iterator exhausted.");
      }
      currentEdge = edgeIterator.next();
      return currentEdge;
    }

    private void primeIterator() {
      while (true) {
        if (edgeIterator != null && edgeIterator.hasNext()) {
          hasNext = true;  // technically, we shouldn't need to put this here, but let's be safe
          return;
        } else if (connectionIterator != null && connectionIterator.hasNext()) {
          Map.Entry<V, List<E>> nextConnection = connectionIterator.next();
          edgeIterator = nextConnection.getValue().iterator();
          currentTarget = nextConnection.getKey();
        } else if (vertexIterator != null && vertexIterator.hasNext()) {
          Map.Entry<V, Map<V, List<E>>> nextVertex = vertexIterator.next();
          connectionIterator = nextVertex.getValue().entrySet().iterator();
          currentSource = nextVertex.getKey();

View on GitHub (pinned to 1b7edd19c4)