stanfordnlp/CoreNLP · error · IllegalArgumentException

Iterator is empty!

Error message

Iterator is empty!

What it means

The concatenated-iterator returned by CollectionUtils (lazy concatenation of a list of iterators) throws IllegalArgumentException from next() when hasNext() is false — i.e. all underlying iterators are exhausted or the iterator list is empty. It signals a misuse: calling next() without a preceding successful hasNext().

Solutions

  1. Always guard calls with if (it.hasNext()) before next().
  2. Use a standard for-each/while(hasNext()) loop rather than manual next() calls.
  3. Check the iterator list is non-empty before iterating; handle the empty case explicitly.
  4. If iterating may run twice, obtain a fresh concatenated iterator for each pass.

Example fix

// before
E e = concatIter.next();
// after
if (concatIter.hasNext()) { E e = concatIter.next(); } else { /* handle empty */ }
Defensive patterns

Strategy: type-guard

Validate before calling

if (iterators == null || iterators.isEmpty()) { /* handle empty before creating/using iterator */ }

Type guard

boolean nextExists(Iterator<?> it) { return it != null && it.hasNext(); }

Try / catch

try { E e = it.next(); } catch (IllegalArgumentException e) { /* iterator exhausted; handle empty */ }

Prevention

When it happens

Trigger: Calling next() on the concatenated iterator after exhaustion; calling next() first in a loop without hasNext(); constructing with an empty iterator list and immediately calling next().

Common situations: Custom iteration code that assumes an element exists; concurrent removal from the source iterators; iterating after a terminal operation already consumed everything.

Understand the failure class

Background: EmptyResultError / "no results found": when an API or scraper succeeds but returns zero rows — this error's family across 9 libraries.

Related errors


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

Appendix: source

Thrown at src/edu/stanford/nlp/util/CollectionUtils.java:920

   * This should respect the remove() functionality of the constituent iterators.
   *
   * @param iterators The iterators to concatenate.
   * @param <E> The type of the iterators.
   * @return An iterator consisting of all the component iterators concatenated together in order.
   */
  @SafeVarargs
  public static <E> Iterator<E> concatIterators(final Iterator<E>... iterators) {
    return new Iterator<E>() {
      Iterator<E> lastIter = null;
      List<Iterator<E>> iters = new LinkedList<>(Arrays.asList(iterators));
      @Override
      public boolean hasNext() {
        return !iters.isEmpty() && iters.get(0).hasNext();
      }
      @Override
      public E next() {
        if (!hasNext()) {
          throw new IllegalArgumentException("Iterator is empty!");
        }
        E next = iters.get(0).next();
        lastIter = iters.get(0);
        while (!iters.isEmpty() && !iters.get(0).hasNext()) {
          iters.remove(0);
        }
        return next;
      }
      @Override
      public void remove() {
        if (lastIter == null) {
          throw new IllegalStateException("Call next() before calling remove()!");
        }
        lastIter.remove();
      }
    };
  }

View on GitHub (pinned to 1b7edd19c4)