spring-projects/spring-framework · error · InvalidPropertyException
Cannot get element with index {index} from Collection of siz
Error message
Cannot get element with index {index} from Collection of size {coll.size()}, accessed using property path '{propertyName}' What it means
Raised in the Iterable/Collection branch of getPropertyValue(PropertyTokenHolder) when the value is a Collection and the requested index is < 0 or >= coll.size(). Spring throws InvalidPropertyException naming the index, the size, and the path. Unlike List, a generic Collection has no positional get(), so an out-of-range index is rejected up front before iterating.
Source
Thrown at spring-beans/src/main/java/org/springframework/beans/AbstractNestablePropertyAccessor.java:650
else if (value instanceof List list) {
int index = Integer.parseInt(key);
growCollectionIfNecessary(list, index, indexedPropertyName.toString(), ph, i + 1);
value = list.get(index);
}
else if (value instanceof Map map) {
Class<?> mapKeyType = ph.getResolvableType().getNested(i + 1).asMap().resolveGeneric(0);
// IMPORTANT: Do not pass full property name in here - property editors
// must not kick in for map keys but rather only for map values.
TypeDescriptor typeDescriptor = TypeDescriptor.valueOf(mapKeyType);
Object convertedMapKey = convertIfNecessary(null, null, key, mapKeyType, typeDescriptor);
value = map.get(convertedMapKey);
}
else if (value instanceof Iterable iterable) {
// Apply index to Iterator in case of a Set/Collection/Iterable.
int index = Integer.parseInt(key);
if (value instanceof Collection<?> coll) {
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 " +View on GitHub (pinned to e8729d0438)
Solutions
- Use a List instead of a Set if positional/indexed access is required.
- Check the collection size before attempting indexed access.
- Access Set members by iteration rather than by index.
Example fix
// before
public class Doc { private Set<String> tags = new HashSet<>(); ... }
Object t = wrapper.getPropertyValue("tags[1]");
// after
public class Doc { private List<String> tags = new ArrayList<>(); ... } Defensive patterns
Strategy: validation
Validate before calling
Object coll = wrapper.getPropertyValue(propertyName.replaceAll("\\[.*$", ""));
int idx = Integer.parseInt(propertyName.replaceAll(".*\\[|\\].*", ""));
if (coll instanceof Collection<?> c && idx >= 0 && idx < c.size()) {
wrapper.getPropertyValue(propertyName);
} Type guard
static boolean indexInCollection(Collection<?> c, int idx) { return idx >= 0 && idx < c.size(); } Try / catch
try { Object v = wrapper.getPropertyValue(propertyName); }
catch (InvalidPropertyException ex) { v = null; } Prevention
- Avoid bracket access on Set; use List for positional reads.
- Bound-check indices against collection size before indexed access.
When it happens
Trigger: getProperty("set[3]") on a Set of size 2; binding 'tags[10]' onto a Set field; reading an indexed value from a Collection that has fewer elements than the index.
Common situations: Using indexed access (brackets) on a Set; binding positional data to unordered collections; misreading a Set as if it were a List.
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 Iterable of size
- 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/db295b3b51b14fc3.json.
Report an issue: GitHub.