apache/hadoop · error · NoSuchElementException

No more entry in {}

Error message

No more entry in {}

What it means

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.

Source

Thrown at hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/fs/Hdfs.java:288

          throw new FileNotFoundException("File " + src + " does not exist.");
        }
        i = 0;
      }
      return (i<thisListing.getPartialListing().length);
    }

    /**
     * Get the next item in the list
     * @return the next item in the list
     * 
     * @throws IOException if there is any error
     * @throws NoSuchElementException if no more entry is available
     */
    public HdfsFileStatus getNext() throws IOException {
      if (hasNext()) {
        return thisListing.getPartialListing()[i++];
      }
      throw new NoSuchElementException("No more entry in " + src);
    }
  }

  /**
   * {@inheritDoc}
   *
   * If any of the the immediate children of the given path f is a symlink, the
   * returned FileStatus object of that children would be represented as a
   * symlink. It will not be resolved to the target path and will not get the
   * target path FileStatus object. The target path will be available via
   * getSymlink on that children's FileStatus object. Since it represents as
   * symlink, isDirectory on that children's FileStatus will return false.
   *
   * If you want to get the FileStatus of target path for that children, you may
   * want to use GetFileStatus API with that children's symlink path. Please see
   * {@link Hdfs#getFileStatus(Path f)}
   */
  @Override

View on GitHub (pinned to 2add963021)

Solutions

  1. Use the canonical idiom: while (it.hasNext()) { X x = it.next(); } with hasNext() evaluated immediately before every next().
  2. Remember hasNext() declares IOException: declare or handle it rather than swallowing, so interruption of the listing is not masked.
  3. Wrap the pattern once in a helper (RemoteIterator<T> to List<T> or Stream<T>) and reuse it everywhere.

Example fix

// before
HdfsFileStatus s;
while ((s = it.next()) != null) { ... }
// NoSuchElementException: No more entry in <dir>

// after
while (it.hasNext()) {
  HdfsFileStatus s = it.next();
  // ...
}
Defensive patterns

Strategy: validation

Validate before calling

// evaluate hasNext() immediately before every next()
if (it.hasNext()) {
  HdfsFileStatus s = it.next();
}

Try / catch

catch (NoSuchElementException e) {
  // bookkeeping bug: replace the loop with while (it.hasNext()) and stop
}

Prevention

When it happens

Trigger: 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.

Common situations: Porting java.util.Iterator code or stream pipelines to RemoteIterator; off-by-one errors in do/while loops; wrappers that consume the iterator twice.

Related errors


AI-assisted analysis of apache/hadoop@2add963021 (2026-08-22). Data as JSON: /api/errors/adf41aa60811f321. Report an issue: GitHub.