{"record":{"id":"d46ba761cefa7e1f","repo":"apache/hadoop","slug":"no-more-entries-in","errorCode":null,"errorMessage":"No more entries in {}","messagePattern":"No more entries in (.+?)","errorType":"exception","errorClass":"NoSuchElementException","httpStatus":null,"severity":"error","filePath":"hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/FileSystem.java","lineNumber":2292,"sourceCode":"   * @throws FileNotFoundException if <code>f</code> does not exist\n   * @throws IOException if any I/O error occurred\n   */\n  protected RemoteIterator<LocatedFileStatus> listLocatedStatus(final Path f,\n      final PathFilter filter)\n  throws FileNotFoundException, IOException {\n    return new RemoteIterator<LocatedFileStatus>() {\n      private final FileStatus[] stats = listStatus(f, filter);\n      private int i = 0;\n\n      @Override\n      public boolean hasNext() {\n        return i<stats.length;\n      }\n\n      @Override\n      public LocatedFileStatus next() throws IOException {\n        if (!hasNext()) {\n          throw new NoSuchElementException(\"No more entries in \" + f);\n        }\n        FileStatus result = stats[i++];\n        // for files, use getBlockLocations(FileStatus, int, int) to avoid\n        // calling getFileStatus(Path) to load the FileStatus again\n        BlockLocation[] locs = result.isFile() ?\n            getFileBlockLocations(result, 0, result.getLen()) :\n            null;\n        return new LocatedFileStatus(result, locs);\n      }\n    };\n  }\n\n  /**\n   * Generic iterator for implementing {@link #listStatusIterator(Path)}.\n   */\n  protected class DirListingIterator<T extends FileStatus> implements\n      RemoteIterator<T> {\n","sourceCodeStart":2274,"sourceCodeEnd":2310,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/FileSystem.java#L2274-L2310","documentation":"NoSuchElementException thrown by the anonymous RemoteIterator<LocatedFileStatus> returned from FileSystem.listLocatedStatus(Path) (listFiles builds on it). The whole listing is materialized once via listStatus(f, filter) into stats; next() checks hasNext() and, once the cursor i reaches stats.length, throws with the queried path in the message. It is a plain Iterator-contract violation by the caller, not an IO failure.","triggerScenarios":"Calling next() without a preceding true hasNext(): while(true){ it.next(); }, calling next() after the loop already consumed the final entry, or unconditionally doing LocatedFileStatus s = it.next() on a possibly-empty directory listing.","commonSituations":"Custom InputFormat/split calculators or Spark/Hive readers adapted from java.util.Iterator code; assuming a directory is non-empty; reusing an iterator after it was drained by an earlier loop. Note the file list is fetched up front, so concurrent deletion does not cause this — it is purely caller logic.","solutions":["Gate every next() with hasNext(): while (it.hasNext()) { LocatedFileStatus s = it.next(); ... }","If you need lookahead, wrap the iterator and buffer one element yourself instead of probing with next()","Do not reuse a drained RemoteIterator; request a fresh one from listLocatedStatus/listFiles"],"exampleFix":"// before\nRemoteIterator<LocatedFileStatus> it = fs.listFiles(dir, true);\nwhile (true) {\n  LocatedFileStatus s = it.next(); // NoSuchElementException on empty dir\n  process(s);\n}\n\n// after\nRemoteIterator<LocatedFileStatus> it = fs.listFiles(dir, true);\nwhile (it.hasNext()) {\n  process(it.next());\n}","handlingStrategy":"validation","validationCode":"RemoteIterator<LocatedFileStatus> it = fs.listFiles(dir, true);\nwhile (it.hasNext()) {          // mandatory guard before every next()\n  LocatedFileStatus st = it.next();\n  process(st);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never call next() outside a while(it.hasNext()) block","Treat empty directories as normal: check hasNext() before the first next()","Create a new iterator per traversal pass; a drained RemoteIterator cannot be reset"],"tags":["hadoop","filesystem","remote-iterator","nosuchelementexception","iterator-misuse","listfiles"],"backgroundTag":"remote-iterator-exhausted","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}