spring-projects/spring-framework · error · InvalidPropertyException
Cannot get element with index {index} from Iterable of size
Error message
Cannot get element with index {index} from Iterable of size {currIndex}, accessed using property path '{propertyName}' What it means
Raised in the Iterable branch when the value is an Iterable (but not a Collection, so size is unknown up front). Spring iterates looking for the element at the requested index; if the iterator is exhausted without finding it, InvalidPropertyException is thrown with 'Iterable of size {currIndex}'. currIndex reflects how many elements were actually seen.
Source
Thrown at spring-beans/src/main/java/org/springframework/beans/AbstractNestablePropertyAccessor.java:667
if (index < 0 || index >= coll.size()) {
throw new InvalidPropertyException(getRootClass(), this.nestedPath + propertyName,
"Cannot get element with index " + index + " from Collection of size " +
coll.size() + ", accessed using property path '" + propertyName + "'");
}
}
Iterator<Object> it = iterable.iterator();
boolean found = false;
int currIndex = 0;
for (; it.hasNext(); currIndex++) {
Object elem = it.next();
if (currIndex == index) {
value = elem;
found = true;
break;
}
}
if (!found) {
throw new InvalidPropertyException(getRootClass(), this.nestedPath + propertyName,
"Cannot get element with index " + index + " from Iterable of size " +
currIndex + ", accessed using property path '" + propertyName + "'");
}
}
else {
throw new InvalidPropertyException(getRootClass(), this.nestedPath + propertyName,
"Property referenced in indexed property path '" + propertyName +
"' is neither an array nor a List/Set/Collection/Iterable nor a Map; " +
"returned value was [" + value + "]");
}
indexedPropertyName.append(PROPERTY_KEY_PREFIX).append(key).append(PROPERTY_KEY_SUFFIX);
}
}
return value;
}
catch (InvalidPropertyException ex) {
throw ex;
}View on GitHub (pinned to e8729d0438)
Solutions
- Use a List (or copy the Iterable into a List) when you need indexed access.
- Verify the Iterable contains enough elements before indexed access.
- Avoid bracket syntax on Stream-backed or lazy iterables.
Example fix
// before
public class Batch { private Iterable<Row> rows; ... } // unknown size
Object r = wrapper.getPropertyValue("rows[3]");
// after
public class Batch { private List<Row> rows = new ArrayList<>(); ... } Defensive patterns
Strategy: validation
Validate before calling
Object it = wrapper.getPropertyValue(propertyName.replaceAll("\\[.*$", ""));
int idx = Integer.parseInt(propertyName.replaceAll(".*\\[|\\].*", ""));
if (it instanceof Collection<?> c && idx >= 0 && idx < c.size()) {
wrapper.getPropertyValue(propertyName);
} else if (!(it instanceof Iterable)) {
throw new IllegalArgumentException("not iterable");
} Type guard
static boolean safeIterableIndex(Object value, int idx) {
if (value instanceof Collection<?> c) return idx >= 0 && idx < c.size();
return false; // non-Collection Iterable: cannot bound-check up front
} Try / catch
try { Object v = wrapper.getPropertyValue(propertyName); }
catch (InvalidPropertyException ex) { v = null; } Prevention
- Copy custom Iterables into Lists before indexed access.
- Avoid bracket syntax on Stream-backed or lazy Iterables.
- Prefer List types on bean fields you intend to index.
When it happens
Trigger: getProperty("stream[5]") on an Iterable backed by a Stream or custom iterator with fewer than 6 elements; reading 'items[2]' from a Queue/deque-like Iterable with only 1 element; binding an index to a non-Collection Iterable.
Common situations: Custom Iterable types, Queue/Deque fields accessed by index; Stream-backed lazy iterables; misusing indexed access on non-List iterables.
Related errors
- Invalid array index in property path '{tokens.canonicalName}
- Invalid list index in property path '{tokens.canonicalName}'
- Cannot get element with index {index} from Collection of siz
- Index of out of bounds in property path '${propertyName}'
- No property handler found
AI-assisted analysis of spring-projects/spring-framework@e8729d0438 (2026-08-04).
Data as JSON: /data/errors/f978ed3313064555.json.
Report an issue: GitHub.