{"record":{"id":"7eb24c7bacf99967","repo":"apache/hadoop","slug":"no-more-items-in-iterator-7eb24c","errorCode":null,"errorMessage":"No more items in iterator","messagePattern":"No more items in iterator","errorType":"exception","errorClass":"NoSuchElementException","httpStatus":null,"severity":"error","filePath":"hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/FileSystem.java","lineNumber":2336,"sourceCode":"    }\n\n    @Override\n    public boolean hasNext() throws IOException {\n      return i < entries.getEntries().length ||\n          entries.hasMore();\n    }\n\n    private void fetchMore() throws IOException {\n      byte[] token = entries.getToken();\n      entries = listStatusBatch(path, token);\n      i = 0;\n    }\n\n    @Override\n    @SuppressWarnings(\"unchecked\")\n    public T next() throws IOException {\n      if (!hasNext()) {\n        throw new NoSuchElementException(\"No more items in iterator\");\n      }\n      if (i == entries.getEntries().length) {\n        fetchMore();\n      }\n      return (T)entries.getEntries()[i++];\n    }\n  }\n\n  /**\n   * Returns a remote iterator so that followup calls are made on demand\n   * while consuming the entries. Each FileSystem implementation should\n   * override this method and provide a more efficient implementation, if\n   * possible.\n   *\n   * Does not guarantee to return the iterator that traverses statuses\n   * of the files in a sorted order.\n   *\n   * @param p target path","sourceCodeStart":2318,"sourceCodeEnd":2354,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/FileSystem.java#L2318-L2354","documentation":"NoSuchElementException from the batching RemoteIterator inner class over listStatusBatch (FileSystem.java:2336) that backs FileSystem.listStatusIterator(Path) and listFilesIterator(Path, boolean). The iterator serves entries from one batch at a time (fetchMore() requests the next batch with a token); next() first asserts hasNext() and throws this fixed message when the cursor passed the final entry — it will not fetch another batch past the end.","triggerScenarios":"Calling next() on the iterator returned by listStatusIterator(path) or listFilesIterator(path, recursive) after the last entry, or without checking hasNext(); sharing one RemoteIterator across threads so one consumer drains it and another calls next() unsynchronized.","commonSituations":"Migrating from the listStatus(Path) array API to iterators for very large directories (Hadoop 3.4 batched listing) and keeping old index-style loops; worker pools consuming a single shared iterator without a handoff queue.","solutions":["Gate every next() with hasNext() — the contract is identical to java.util.Iterator","For concurrent consumers, drain the RemoteIterator on one thread into a BlockingQueue, or create one iterator per thread","Prefer listStatusIterator/listFilesIterator over hand-rolling listStatusBatch token loops"],"exampleFix":"// before\nRemoteIterator<FileStatus> it = fs.listStatusIterator(dir);\nFileStatus first = it.next(); // throws on empty directory\n\n// after\nRemoteIterator<FileStatus> it = fs.listStatusIterator(dir);\nFileStatus first = it.hasNext() ? it.next() : null;","handlingStrategy":"validation","validationCode":"RemoteIterator<FileStatus> it = fs.listStatusIterator(dir);\nFileStatus first = it.hasNext() ? it.next() : null; // guarded first element\nwhile (it.hasNext()) {\n  process(it.next());\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Gate every next() with hasNext() — the batch iterator throws rather than fetching past the end","Consume a RemoteIterator from one thread; hand entries to workers via a queue","Do not port array-index loops (i < list.length) onto iterators without adding the hasNext() check"],"tags":["hadoop","filesystem","remote-iterator","nosuchelementexception","batch-listing","iterator-misuse"],"backgroundTag":"remote-iterator-exhausted","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-23T01:17:44.959Z"}