apache/iceberg · error · IllegalArgumentException
Cannot set value
Error message
Cannot set value
What it means
Pair.put is a StructLike positional setter that only accepts positions 0 (first) or 1 (second). Passing any other index is an API contract violation, so it throws IllegalArgumentException with the offending index and value.
Source
Thrown at core/src/main/java/org/apache/iceberg/util/Pair.java:76
private X first;
private Y second;
private Pair(X first, Y second) {
this.first = first;
this.second = second;
}
@Override
@SuppressWarnings("unchecked")
public void put(int i, Object v) {
if (i == 0) {
this.first = (X) v;
return;
} else if (i == 1) {
this.second = (Y) v;
return;
}
throw new IllegalArgumentException("Cannot set value " + i + " (not 0 or 1): " + v);
}
@Override
public Object get(int i) {
if (i == 0) {
return first;
} else if (i == 1) {
return second;
}
throw new IllegalArgumentException("Cannot get value " + i + " (not 0 or 1)");
}
@Override
public Schema getSchema() {
if (schema == null) {
this.schema = SCHEMA_CACHE.get(Pair.of(first.getClass(), second.getClass()));
}
return schema;View on GitHub (pinned to 86d9c8fc54)
Solutions
- Only call put with index 0 or 1
- Use the first()/second() accessors or the Pair.of factory instead of positional access
- Fix the loop bound in generic StructLike handling code to use the record's size
Example fix
// before pair.put(2, value); // throws // after pair.put(value instanceof Integer ? 0 : 1, value);
Defensive patterns
Strategy: validation
Validate before calling
if (pos < 0 || pos > 1) throw new IllegalArgumentException("Pair position must be 0 or 1, got " + pos); Type guard
boolean isValidPairPosition(int i) { return i == 0 || i == 1; } Try / catch
try { pair.put(pos, v); } catch (IllegalArgumentException e) { /* log index and fix caller */ } Prevention
- Prefer first()/second() over positional access
- Use Pair.of(...) factories
- Bound generic StructLike loops with the actual schema size
When it happens
Trigger: Calling put(int pos, Object value) on a Pair with pos outside {0,1}, e.g. from generic StructLike code that assumes a different arity.
Common situations: Generic structural-copy code iterating over field counts that doesn't match Pair's fixed 2-field schema; reflection-based writers.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- Cannot get value
- Can't retrieve values from an empty struct
- Can't modify an empty struct
- Cannot set fields in a TypeProjection
- Setting values is not supported
AI-assisted analysis of apache/iceberg@86d9c8fc54 (2026-09-12).
Data as JSON: /api/errors/f9ad915f4026f975.
Report an issue: GitHub.