{"record":{"id":"4b2bee6265b0ef5b","repo":"apache/hadoop","slug":"no-more-items-in-iterator-4b2bee","errorCode":null,"errorMessage":"No more items in iterator.","messagePattern":"No more items in iterator\\.","errorType":"exception","errorClass":"NoSuchElementException","httpStatus":null,"severity":"error","filePath":"hadoop-cloud-storage-project/hadoop-tos/src/main/java/org/apache/hadoop/fs/tosfs/util/Iterables.java","lineNumber":84,"sourceCode":"\n        while (iterator.hasNext()) {\n          value = iterator.next();\n          if (predicate.test(value)) {\n            advance = false;\n            return true;\n          }\n        }\n\n        return false;\n      }\n\n      @Override\n      public T next() {\n        if (hasNext()) {\n          advance = true;\n          return value;\n        }\n        throw new NoSuchElementException(\"No more items in iterator.\");\n      }\n    };\n  }\n\n  public static <T extends @Nullable Object> Iterable<T> concat(\n      Iterable<? extends Iterable<? extends T>> inputs) {\n    return () -> new ConcatenatedIterator<>(inputs.iterator());\n  }\n\n  private static class ConcatenatedIterator<T> implements Iterator<T> {\n    // Iterators is the iterator of iterables.\n    private final Iterator<? extends Iterable<? extends T>> iterators;\n    private Iterator<? extends T> curIter;\n\n    ConcatenatedIterator(Iterator<? extends Iterable<? extends T>> iterators) {\n      Preconditions.checkNotNull(iterators, \"Iterators should not be null.\");\n      this.iterators = iterators;\n    }","sourceCodeStart":66,"sourceCodeEnd":102,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-cloud-storage-project/hadoop-tos/src/main/java/org/apache/hadoop/fs/tosfs/util/Iterables.java#L66-L102","documentation":"Iterables wraps and transforms iterators for the hadoop-tos connector; this next() implementation caches the element produced by hasNext() and throws NoSuchElementException('No more items in iterator.') when next() is called after the underlying iteration is exhausted. In other words hasNext() was false (or was never consulted) and the caller still demanded an element — a strict iterator-protocol violation, not a storage problem.","triggerScenarios":"Calling it.next() without checking it.hasNext(), or calling next() a second time after a final hasNext()==false in loops over Iterables.transformed/filtered results, e.g. object listing helpers that iterate keys or upload parts.","commonSituations":"Hand-written for(;;) loops using next() alone; code that assumes at least one element exists (first()/single-item expectations) on an empty listing; off-by-one loop counters over connector-returned Iterables; refactors that removed a hasNext() guard.","solutions":["Guard every next() with hasNext(): while (it.hasNext()) { T v = it.next(); ... }","For 'expect exactly one element' logic, use hasNext() to branch explicitly instead of calling next() unconditionally.","Search the stack trace for your loop frame — the connector class is fine; the defect is at the caller.","If using Java 8+, prefer stream(Iterable).findFirst()/collect instead of manual iteration to eliminate the pattern."],"exampleFix":"// before\nT first = it.next(); // throws on empty iterable\n\n// after\nif (!it.hasNext()) { throw new IllegalStateException(...); }\nT first = it.next();","handlingStrategy":"validation","validationCode":"Iterator<T> it = iterable.iterator();\nwhile (it.hasNext()) {\n  T v = it.next();\n  process(v);\n}","typeGuard":null,"tryCatchPattern":"try { first = it.next(); }\ncatch (NoSuchElementException e) { first = defaultValue; }","preventionTips":["Never call next() without a preceding hasNext() check.","Prefer streams or for-each over manual iterators.","For single-element expectations, branch on hasNext() explicitly."],"tags":["iterator","no-such-element-exception","programming-error","collections"],"backgroundTag":"iterator-next-past-end","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-23T01:17:44.959Z"}