apache/beam · warning · NoSuchElementException
NoSuchElementException
Error message
NoSuchElementException
What it means
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().
Source
Thrown at sdks/java/core/src/main/java/org/apache/beam/sdk/fn/data/BeamFnDataInboundObserver.java:145
// Copies the elements of list to an array and removes references to elements that
// have been iterated past.
private static class DiscardingIterator<T> implements Iterator<T> {
private int index = 0;
private final @Nullable Object[] array;
DiscardingIterator(List<T> list) {
this.array = list.toArray();
}
@Override
public boolean hasNext() {
return index < array.length;
}
@Override
public T next() {
if (index >= array.length) {
throw new NoSuchElementException();
}
@SuppressWarnings("unchecked")
T result = (T) array[index];
array[index] = null;
++index;
return result;
}
}
private static <T> Iterator<T> createDiscardingIterator(List<T> list) {
if (list.isEmpty()) {
return Collections.emptyIterator(); // Optimize empty lists, which are common for timers.
}
return new DiscardingIterator<>(list);
}
/**
* Uses the callers thread to process all elements received until we receive the end of the streamView on GitHub (pinned to 12126d8942)
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.
Example fix
// before
while (true) { Data d = it.next(); ... }
// after
while (it.hasNext()) { Data d = it.next(); ... } Defensive patterns
Strategy: validation
Validate before calling
while (it.hasNext()) { T v = it.next(); } Try / catch
try { v = it.next(); } catch (NoSuchElementException e) { /* overrun */ } Prevention
- Always check hasNext() before next()
- Prefer for-each loops
- Don't cache next() results across loop boundaries
When it happens
Trigger: 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.
Common situations: Hand-written iteration loops over Elements.Data/Elements.Timers lists; off-by-one when mixing hasNext/next calls.
Understand the failure class
Background: EmptyResultError / "no results found": when an API or scraper succeeds but returns zero rows — this error's family across 9 libraries.
Related errors
- Instruction id was poisoned
- Unregistering consumer which was not registered.
- PoisonedException
- UnsupportedOperationException
- Unable to find inbound data receiver for instruction %s and
AI-assisted analysis of apache/beam@12126d8942 (2026-09-13).
Data as JSON: /api/errors/1cd095aaa3df998e.
Report an issue: GitHub.