{"record":{"id":"adf41aa60811f321","repo":"apache/hadoop","slug":"no-more-entry-in","errorCode":null,"errorMessage":"No more entry in {}","messagePattern":"No more entry in (.+?)","errorType":"exception","errorClass":"NoSuchElementException","httpStatus":null,"severity":"error","filePath":"hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/fs/Hdfs.java","lineNumber":288,"sourceCode":"          throw new FileNotFoundException(\"File \" + src + \" does not exist.\");\n        }\n        i = 0;\n      }\n      return (i<thisListing.getPartialListing().length);\n    }\n\n    /**\n     * Get the next item in the list\n     * @return the next item in the list\n     * \n     * @throws IOException if there is any error\n     * @throws NoSuchElementException if no more entry is available\n     */\n    public HdfsFileStatus getNext() throws IOException {\n      if (hasNext()) {\n        return thisListing.getPartialListing()[i++];\n      }\n      throw new NoSuchElementException(\"No more entry in \" + src);\n    }\n  }\n\n  /**\n   * {@inheritDoc}\n   *\n   * If any of the the immediate children of the given path f is a symlink, the\n   * returned FileStatus object of that children would be represented as a\n   * symlink. It will not be resolved to the target path and will not get the\n   * target path FileStatus object. The target path will be available via\n   * getSymlink on that children's FileStatus object. Since it represents as\n   * symlink, isDirectory on that children's FileStatus will return false.\n   *\n   * If you want to get the FileStatus of target path for that children, you may\n   * want to use GetFileStatus API with that children's symlink path. Please see\n   * {@link Hdfs#getFileStatus(Path f)}\n   */\n  @Override","sourceCodeStart":270,"sourceCodeEnd":306,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/fs/Hdfs.java#L270-L306","documentation":"DirListingIterator.getNext() (backs RemoteIterator.next()) enforces the iterator contract: once hasNext() has returned false, there are no more entries and the next next() call throws NoSuchElementException naming the listed directory. This is a local API-contract violation, not an HDFS service condition; note RemoteIterator also lets hasNext()/next() throw IOException, unlike java.util.Iterator.","triggerScenarios":"Calling it.next() without a matching it.hasNext() check; calling next() again after a false hasNext(); ported loops of the form while(true){ next(); } or do { next(); } while(...); assuming next() returns null at the end.","commonSituations":"Porting java.util.Iterator code or stream pipelines to RemoteIterator; off-by-one errors in do/while loops; wrappers that consume the iterator twice.","solutions":["Use the canonical idiom: while (it.hasNext()) { X x = it.next(); } with hasNext() evaluated immediately before every next().","Remember hasNext() declares IOException: declare or handle it rather than swallowing, so interruption of the listing is not masked.","Wrap the pattern once in a helper (RemoteIterator<T> to List<T> or Stream<T>) and reuse it everywhere."],"exampleFix":"// before\nHdfsFileStatus s;\nwhile ((s = it.next()) != null) { ... }\n// NoSuchElementException: No more entry in <dir>\n\n// after\nwhile (it.hasNext()) {\n  HdfsFileStatus s = it.next();\n  // ...\n}","handlingStrategy":"validation","validationCode":"// evaluate hasNext() immediately before every next()\nif (it.hasNext()) {\n  HdfsFileStatus s = it.next();\n}","typeGuard":null,"tryCatchPattern":"catch (NoSuchElementException e) {\n  // bookkeeping bug: replace the loop with while (it.hasNext()) and stop\n}","preventionTips":["Use the while (it.hasNext()) idiom exclusively with RemoteIterator.","Never expect null from next(): RemoteIterator signals exhaustion only via hasNext() == false.","Wrap iterator consumption in one shared utility so the contract lives in one place."],"tags":["hdfs","remote-iterator","no-such-element","api-contract","programming-error"],"backgroundTag":"iterator-exhausted","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}