apache/hadoop · error · IllegalArgumentException
name + " cannot be negative but was: " + value
Error message
name + " cannot be negative but was: " + value
What it means
org.apache.hadoop.util.Lists is Hadoop's internal near-copy of Guava's Lists. Before sizing a backing array via computeArrayListCapacity (saturatedCast of 5L + arraySize + arraySize/10), checkNonnegative rejects a negative arraySize with IllegalArgumentException("arraySize cannot be negative but was: N"). Zero is legal; only negatives throw.
Source
Thrown at hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/Lists.java:199
* @param elements elements.
* @param <E> Generics Type E.
* @return Generics Type E List.
*/
public static <E> LinkedList<E> newLinkedList(
Iterable<? extends E> elements) {
LinkedList<E> list = newLinkedList();
addAll(list, elements);
return list;
}
private static int computeArrayListCapacity(int arraySize) {
checkNonnegative(arraySize, "arraySize");
return saturatedCast(5L + arraySize + (arraySize / 10));
}
private static int checkNonnegative(int value, String name) {
if (value < 0) {
throw new IllegalArgumentException(name + " cannot be negative but was: "
+ value);
}
return value;
}
/**
* Returns the {@code int} nearest in value to {@code value}.
*
* @param value any {@code long} value.
* @return the same value cast to {@code int} if it is in the range of the
* {@code int} type, {@link Integer#MAX_VALUE} if it is too large,
* or {@link Integer#MIN_VALUE} if it is too small.
*/
private static int saturatedCast(long value) {
if (value > Integer.MAX_VALUE) {
return Integer.MAX_VALUE;
}
if (value < Integer.MIN_VALUE) {View on GitHub (pinned to 2add963021)
Solutions
- Clamp the estimate before the call: Math.max(0, n)
- Use newArrayListWithExpectedSize or plain newArrayList() when the capacity is unknown
- Fix the upstream expression that can go negative and add a unit test for the empty-input case
Example fix
// before List<String> l = Lists.newArrayListWithCapacity(expected - RESERVE); // after List<String> l = Lists.newArrayListWithCapacity(Math.max(0, expected - RESERVE));
Defensive patterns
Strategy: validation
Validate before calling
int size = Math.max(0, computeEstimate(...)); List<String> list = size > 0 ? Lists.newArrayListWithCapacity(size) : Lists.<String>newArrayList();
Prevention
- Clamp size arithmetic with Math.max(0, ...) before collection construction
- Never pass config '-1 means unset' sentinels through to sizing APIs
- Favor newArrayListWithExpectedSize when the value is an estimate, not a capacity
When it happens
Trigger: Lists.newArrayListWithCapacity(n) with n < 0 — typically n from an estimate that underflowed (expectedSize - reserved, size/2 - x) or a config-parsed capacity with a negative 'unset' sentinel.
Common situations: Copied Guava idioms resolving to Hadoop's Lists; expected-size arithmetic on empty or tiny inputs; forwarding a -1 default from an unset configuration key.
Related errors
- Illegal option {}
- Not enough arguments: expected {} but got {}
- Too many arguments: expected {} but got {}
- Target path not specified. <target path> <src path> <src pat
- The number of source paths is less than 2. <target path> <sr
AI-assisted analysis of apache/hadoop@2add963021 (2026-08-22).
Data as JSON: /api/errors/4c31305907e1daa9.
Report an issue: GitHub.