apache/hadoop · error · IllegalStateException

There are no more elements

Error message

There are no more elements

What it means

The GSet iterator's next() throws IllegalStateException("There are no more elements") when called with no elements left. This differs from java.util.Iterator, which throws NoSuchElementException - so catch blocks written for the JDK type silently miss this one. hasNext() must gate every next().

Source

Thrown at hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/LightWeightGSet.java:339

        return;
      }
      next = cur.getNext();
      if (next == null) {
        next = nextNonemptyEntry();
      }
    }

    @Override
    public boolean hasNext() {
      ensureNext();
      return next != null;
    }

    @Override
    public E next() {
      ensureNext();
      if (next == null) {
        throw new IllegalStateException("There are no more elements");
      }
      cur = next;
      next = null;
      return convert(cur);
    }

    @SuppressWarnings("unchecked")
    @Override
    public void remove() {
      ensureNext();
      if (cur == null) {
        throw new IllegalStateException("There is no current element " +
            "to remove");
      }
      LightWeightGSet.this.remove((K)cur);
      iterModification++;
      cur = null;
    }

View on GitHub (pinned to 2add963021)

Solutions

  1. Rewrite the loop as while (it.hasNext()) { E e = it.next(); ... }.
  2. If over-advancement must be tolerated, catch IllegalStateException - but the loop logic is the real bug.
  3. When porting code from java.util collections, remember GSet's next() uses IllegalStateException, not NoSuchElementException.

Example fix

// before
E e = it.next(); // may be past the end

// after
while (it.hasNext()) {
  E e = it.next();
  ...
}
Defensive patterns

Strategy: validation

Validate before calling

while (it.hasNext()) {
  E e = it.next(); // safe: guarded by hasNext()
}

Prevention

When it happens

Trigger: Calling next() after the iterator is exhausted, or calling next() repeatedly without checking hasNext() in between.

Common situations: Hand-rolled loops migrated from other collections; code that conditionally consumed one element and then calls next() unconditionally; catch (NoSuchElementException) clauses that do not match this exception type.

Related errors


AI-assisted analysis of apache/hadoop@2add963021 (2026-08-22). Data as JSON: /api/errors/b8da8fc21b6fc19b. Report an issue: GitHub.