xai-org/x-algorithm · error · NoSuchElementException
collection is empty
Error message
collection is empty
What it means
Min.apply throws NoSuchElementException("collection is empty") when the input collection is empty, since min stays null after iterating. Same semantics as Max and standard empty-aggregate behavior.
Source
Thrown at botmaker/src/java/com/twitter/botmaker/function/math/Min.java:58
public Signature getSignature() {
return SIGNATURE;
}
@Override
protected CacheLevel getCacheLevel() {
return CacheLevel.Global;
}
@Override
protected Object apply(Context<Runtime> context, List<Number> numbers) {
Number min = null;
for (Number num : numbers) {
if (min == null || num.doubleValue() < min.doubleValue()) {
min = num;
}
}
if (min == null) {
throw new NoSuchElementException("collection is empty");
}
return min;
}
}
View on GitHub (pinned to 24c60942c5)
Solutions
- Guard with an emptiness check and supply a default (e.g. If(IsEmpty(list), default, Min(list)))
- Ensure upstream filters/windows guarantee at least one element
- Catch NoSuchElementException only as a last resort at pipeline level
Example fix
// before Number m = Min().apply(ctx, numbers); // empty -> NoSuchElementException // after Number m = numbers.isEmpty() ? Double.MAX_VALUE : (Number) Min().apply(ctx, numbers);
Defensive patterns
Strategy: validation
Validate before calling
if (numbers == null || numbers.isEmpty()) {
return DEFAULT_VALUE;
}
return Min().apply(ctx, numbers); Type guard
public static boolean nonEmpty(Collection<?> c) { return c != null && !c.isEmpty(); } Try / catch
try {
return (Number) Min().apply(ctx, numbers);
} catch (NoSuchElementException e) {
return emptyDefault;
} Prevention
- Guard aggregates with If(IsEmpty(list), default, Min(list)) in expressions
- Validate window/group boundaries so ranges contain at least one row
- Treat empty-collection aggregation as an explicit business case, not an error
When it happens
Trigger: Calling Min() with an empty collection: Min().apply(ctx, ImmutableList.of()), or an expression computing a minimum over an empty window/group.
Common situations: Empty time windows, empty filter results, optional/nullable lists evaluated to empty, off-by-one range boundaries.
Related errors
- collection is empty
- cannot pass empty collection to Foldl1()
- Invalid numeric type (%s)
- expecting a non-negative number for Sqrt() function but rece
AI-assisted analysis of xai-org/x-algorithm@24c60942c5 (2026-08-28).
Data as JSON: /api/errors/b39a9d9b67297485.
Report an issue: GitHub.