apache/hadoop · error · IllegalStateException

There is no current element to remove

Error message

There is no current element to remove

What it means

Iterator remove() throws IllegalStateException("There is no current element to remove") when cur == null: either remove() was called before any next(), or remove() was called twice without an intervening next() (a successful remove clears cur).

Source

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

    }

    @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;
    }

    public void setTrackModification(boolean trackModification) {
      this.trackModification = trackModification;
    }
  }
  
  /**
   * Let t = percentage of max memory.
   * Let e = round(log_2 t).
   * Then, we choose capacity = 2^e/(size of reference),
   * unless it is outside the close interval [1, 2^30].
   *

View on GitHub (pinned to 2add963021)

Solutions

  1. Call next() first, and remove only the element it just returned.
  2. Ensure at most one remove() per next().
  3. In retry logic, track whether the previous remove() succeeded instead of calling it again.

Example fix

// before
while (it.hasNext()) {
  it.remove(); // no next() first -> IllegalStateException
}

// after
while (it.hasNext()) {
  E e = it.next();
  if (shouldDrop(e)) {
    it.remove(); // exactly one remove per next()
  }
}
Defensive patterns

Strategy: validation

Validate before calling

E e = it.next();
if (shouldDrop(e)) {
  it.remove(); // remove() only immediately after a successful next()
}

Prevention

When it happens

Trigger: Calling it.remove() before the first it.next(); or two consecutive it.remove() calls with no next() between them.

Common situations: Filter loops trying to remove the 'current' element before advancing; copy-paste of remove() into a loop containing a conditional next(); retry logic that re-invokes remove() after it already succeeded.

Related errors


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