{"record":{"id":"1cd095aaa3df998e","repo":"apache/beam","slug":"nosuchelementexception","errorCode":null,"errorMessage":"NoSuchElementException","messagePattern":"NoSuchElementException","errorType":"exception","errorClass":"NoSuchElementException","httpStatus":null,"severity":"warning","filePath":"sdks/java/core/src/main/java/org/apache/beam/sdk/fn/data/BeamFnDataInboundObserver.java","lineNumber":145,"sourceCode":"  // Copies the elements of list to an array and removes references to elements that\n  // have been iterated past.\n  private static class DiscardingIterator<T> implements Iterator<T> {\n    private int index = 0;\n    private final @Nullable Object[] array;\n\n    DiscardingIterator(List<T> list) {\n      this.array = list.toArray();\n    }\n\n    @Override\n    public boolean hasNext() {\n      return index < array.length;\n    }\n\n    @Override\n    public T next() {\n      if (index >= array.length) {\n        throw new NoSuchElementException();\n      }\n      @SuppressWarnings(\"unchecked\")\n      T result = (T) array[index];\n      array[index] = null;\n      ++index;\n      return result;\n    }\n  }\n\n  private static <T> Iterator<T> createDiscardingIterator(List<T> list) {\n    if (list.isEmpty()) {\n      return Collections.emptyIterator(); // Optimize empty lists, which are common for timers.\n    }\n    return new DiscardingIterator<>(list);\n  }\n\n  /**\n   * Uses the callers thread to process all elements received until we receive the end of the stream","sourceCodeStart":127,"sourceCodeEnd":163,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/core/src/main/java/org/apache/beam/sdk/fn/data/BeamFnDataInboundObserver.java#L127-L163","documentation":"The iterator returned by BeamFnDataInboundObserver's Elements queue enforces the Iterator contract: calling next() past the end of the current batch array throws NoSuchElementException. Callers must check hasNext() before next().","triggerScenarios":"Calling next() on the data/timer element iterator without checking hasNext(), or calling it more times than there are elements in the current Elements message.","commonSituations":"Hand-written iteration loops over Elements.Data/Elements.Timers lists; off-by-one when mixing hasNext/next calls.","solutions":["Always guard next() with hasNext().","Prefer enhanced-for or stream iteration over manual index handling.","Catch NoSuchElementException to detect iteration overrun defensively in generic code."],"exampleFix":"// before\nwhile (true) { Data d = it.next(); ... }\n// after\nwhile (it.hasNext()) { Data d = it.next(); ... }","handlingStrategy":"validation","validationCode":"while (it.hasNext()) { T v = it.next(); }","typeGuard":null,"tryCatchPattern":"try { v = it.next(); } catch (NoSuchElementException e) { /* overrun */ }","preventionTips":["Always check hasNext() before next()","Prefer for-each loops","Don't cache next() results across loop boundaries"],"tags":["java","iterator","beam-fn"],"backgroundTag":"empty-result-set","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-15T02:17:10.978Z"}