{"record":{"id":"db45c7d5deca9184","repo":"stanfordnlp/CoreNLP","slug":"filesequentialcollection-exhausted","errorCode":null,"errorMessage":"FileSequentialCollection exhausted","messagePattern":"FileSequentialCollection exhausted","errorType":"exception","errorClass":"NoSuchElementException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/io/FileSequentialCollection.java","lineNumber":282,"sourceCode":"      if (roots.length > 0) {\n        fileArrayStack.add(roots[rootsIndex]);\n        fileArrayStackIndices.push(Integer.valueOf(0));\n      }\n      next = primeNextFile();\n    }\n\n    @Override\n    public boolean hasNext() {\n      return next != null;\n    }\n\n    /**\n     * Returns the next element in the iteration.\n     */\n    @Override\n    public File next() {\n      if (next == null) {\n        throw new NoSuchElementException(\"FileSequentialCollection exhausted\");\n      }\n      File ret = next;\n      next = primeNextFile();\n      return ret;\n    }\n\n    /**\n     * Not supported\n     */\n    @Override\n    public void remove() {\n      throw new UnsupportedOperationException();\n    }\n\n    /**\n     * Returns the next file to be accessed, or {@code null} if\n     * there are none left.  This is all quite hairy to write as an\n     * iterator....","sourceCodeStart":264,"sourceCodeEnd":300,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/io/FileSequentialCollection.java#L264-L300","documentation":"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().","triggerScenarios":"Calling next() on a FileSequentialCollection iterator after iteration has finished, or on an empty collection (root path yielded no files) without first checking hasNext().","commonSituations":"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.","solutions":["Always call hasNext() before next() and stop iterating when it returns false.","Use a for-each loop over the FileSequentialCollection, which calls hasNext() automatically.","If the collection is unexpectedly empty, verify the root path exists and is readable (directory or file) before constructing the collection.","Create a new FileSequentialCollection/iterator instead of reusing an exhausted one."],"exampleFix":"// before\nwhile (true) {\n  File f = it.next();\n  process(f);\n}\n// after\nwhile (it.hasNext()) {\n  File f = it.next();\n  process(f);\n}","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"boolean hasFile(Iterator<File> it) { return it.hasNext(); }","tryCatchPattern":"try {\n  while (it.hasNext()) {\n    File f = it.next();\n    process(f);\n  }\n} catch (NoSuchElementException e) {\n  // exhausted iterator reused; restart iteration with a new iterator\n  it = files.iterator();\n}","preventionTips":["Never call next() without hasNext(); prefer enhanced for-loops over FileSequentialCollection.","Check that the root file/directory passed to the constructor exists and is non-empty before iterating.","Treat iterators as single-use; create a fresh iterator instead of reusing an exhausted one.","Log the collection size (or verify non-emptiness) at the start of batch jobs."],"tags":["iterator","nosuchelement","io"],"backgroundTag":"empty-result-set","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}