stanfordnlp/CoreNLP · error · NoSuchElementException

FileSequentialCollection exhausted

Error message

FileSequentialCollection exhausted

What it means

FileSequentialCollection is an Iterator<File>/Collection over files discovered lazily from a root path. Its next() method throws this NoSuchElementException when the internal lookahead field 'next' is null, meaning all files have already been returned and next() was called again. Callers must guard with hasNext().

Solutions

  1. Always call hasNext() before next() and stop iterating when it returns false.
  2. Use a for-each loop over the FileSequentialCollection, which calls hasNext() automatically.
  3. If the collection is unexpectedly empty, verify the root path exists and is readable (directory or file) before constructing the collection.
  4. Create a new FileSequentialCollection/iterator instead of reusing an exhausted one.

Example fix

// before
while (true) {
  File f = it.next();
  process(f);
}
// after
while (it.hasNext()) {
  File f = it.next();
  process(f);
}
Defensive patterns

Strategy: type-guard

Type guard

boolean hasFile(Iterator<File> it) { return it.hasNext(); }

Try / catch

try {
  while (it.hasNext()) {
    File f = it.next();
    process(f);
  }
} catch (NoSuchElementException e) {
  // exhausted iterator reused; restart iteration with a new iterator
  it = files.iterator();
}

Prevention

When it happens

Trigger: Calling next() on a FileSequentialCollection iterator after iteration has finished, or on an empty collection (root path yielded no files) without first checking hasNext().

Common situations: Loops written as do { it.next() } while (...) or while (true) without hasNext(); iterating a directory that resolves to zero files; reusing an exhausted iterator to try to 'reset' it.

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/db45c7d5deca9184. Report an issue: GitHub.

Appendix: source

Thrown at src/edu/stanford/nlp/io/FileSequentialCollection.java:282

      if (roots.length > 0) {
        fileArrayStack.add(roots[rootsIndex]);
        fileArrayStackIndices.push(Integer.valueOf(0));
      }
      next = primeNextFile();
    }

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

    /**
     * Returns the next element in the iteration.
     */
    @Override
    public File next() {
      if (next == null) {
        throw new NoSuchElementException("FileSequentialCollection exhausted");
      }
      File ret = next;
      next = primeNextFile();
      return ret;
    }

    /**
     * Not supported
     */
    @Override
    public void remove() {
      throw new UnsupportedOperationException();
    }

    /**
     * Returns the next file to be accessed, or {@code null} if
     * there are none left.  This is all quite hairy to write as an
     * iterator....

View on GitHub (pinned to 1b7edd19c4)