apache/hadoop · error · NoSuchElementException
No more entry in {f}
Error message
No more entry in {f} What it means
The RemoteIterator<LocatedFileStatus> returned by listLocatedStatus wraps a plain listing iterator; calling next() after hasNext() returned false throws NoSuchElementException with the listed path ('No more entry in {f}'). This is ordinary iterator exhaustion, made easy to miss because RemoteIterator.next() declares IOException as well and loops often only catch that.
Source
Thrown at hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/AbstractFileSystem.java:1186
* @throws UnresolvedLinkException unresolved link exception.
* @throws IOException raised on errors performing I/O.
* @return FileStatus Iterator.
*/
public RemoteIterator<LocatedFileStatus> listLocatedStatus(final Path f)
throws AccessControlException, FileNotFoundException,
UnresolvedLinkException, IOException {
return new RemoteIterator<LocatedFileStatus>() {
private RemoteIterator<FileStatus> itor = listStatusIterator(f);
@Override
public boolean hasNext() throws IOException {
return itor.hasNext();
}
@Override
public LocatedFileStatus next() throws IOException {
if (!hasNext()) {
throw new NoSuchElementException("No more entry in " + f);
}
FileStatus result = itor.next();
BlockLocation[] locs = null;
if (result.isFile()) {
locs = getFileBlockLocations(
result.getPath(), 0, result.getLen());
}
return new LocatedFileStatus(result, locs);
}
};
}
/**
* The specification of this method matches that of
* {@link FileContext.Util#listStatus(Path)} except that Path f must be
* for this file system.
* @param f the path.
* @throws AccessControlException access control exception.View on GitHub (pinned to 2add963021)
Solutions
- Always guard consumption: while (it.hasNext()) { LocatedFileStatus s = it.next(); ... }
- If an unguarded loop must be kept, catch NoSuchElementException separately from IOException as the termination signal
- For small directories, prefer fc.util().listStatus(f) and iterate the returned array
Example fix
// before
while (true) {
LocatedFileStatus s = itor.next(); // throws at end
process(s);
}
// after
while (itor.hasNext()) {
process(itor.next());
} Defensive patterns
Strategy: validation
Validate before calling
while (itor.hasNext()) {
LocatedFileStatus s = itor.next();
// process s
} Prevention
- Never call next() on a RemoteIterator without a preceding hasNext() check
- Remember RemoteIterator.next() throws NoSuchElementException (unchecked), not IOException, at exhaustion
- Prefer util().listStatus(f) for small directories where array semantics are simpler
When it happens
Trigger: Calling next() without a preceding hasNext() check; do/while loops that consume next() first and test hasNext() afterwards; iteration helpers that assume next() returns null at the end instead of throwing.
Common situations: Hand-rolled listing loops ported from java.util.Iterator idioms; merging several RemoteIterators in one loop; input formats and directory walkers iterating large listings where exhaustion is the normal exit path.
Related errors
- No more items in iterator
- Attempted to seek or read past the end of the file
- No more items in iterator.
- No more elements
- Cannot rename symlink {src} to its target {dst}
AI-assisted analysis of apache/hadoop@2add963021 (2026-08-22).
Data as JSON: /api/errors/25ae2cf8db509c8a.
Report an issue: GitHub.