spring-projects/spring-framework · error · InvalidPropertyException
Cannot get element with index
Error message
Cannot get element with index {index} from Collection of size {coll.size()}, accessed using property path '{propertyName}' What it means
Thrown by getPropertyValue in the Collection branch (Set/Collection indexed read) when the requested index is < 0 or >= coll.size(). Sets and generic Collections have no positional access, so Spring iterates; before iterating it bounds-checks the size against the index for a clear error.
Solutions
- Change the field to a List if positional access is required (List preserves insertion order and supports get).
- Bounds-check the index against collection.size() before issuing the indexed read.
- If the Set is intentional, expose iteration rather than positional access in the API.
- Reject/normalize out-of-range indices in the controller or validator.
Example fix
// before
private Set<String> tags = new HashSet<>(); // size 1
wrapper.getPropertyValue("tags[3]");
// after
private List<String> tags = new ArrayList<>();
wrapper.getPropertyValue("tags[3]"); // with size >= 4 Defensive patterns
Strategy: validation
Validate before calling
BeanWrapper w = new BeanWrapperImpl(bean);
Object coll = w.getPropertyValue("tags");
if (coll instanceof Collection<?> c && (idx < 0 || idx >= c.size())) {
throw new IndexOutOfBoundsException("set has " + c.size() + " elements; requested " + idx);
}
w.getPropertyValue("tags[" + idx + "]"); Type guard
static boolean indexInCollection(Object coll, int idx) {
return coll instanceof Collection<?> c && idx >= 0 && idx < c.size();
} Try / catch
try {
return wrapper.getPropertyValue("tags[" + idx + "]");
} catch (InvalidPropertyException ex) {
if (ex.getMessage().contains("from Collection of size")) return null; // out of range
throw ex;
} Prevention
- Use a List instead of a Set whenever positional access is required.
- Bounds-check indices against collection.size() before indexed reads.
- Document that Set ordering is iteration-defined, not index-stable.
When it happens
Trigger: getPropertyValue("set[3]") on a Set/Collection of size <= 3 (or negative index).
Common situations: Binding/reading indexed positions on a Set (order may be undefined); client expecting list semantics on a Set field; index derived from request params that exceeds collection size.
Related errors
- Cannot get element with index
- Invalid property ' ' of bean class [ ]: Index of out of…
- Cannot access indexed value of property referenced in…
- Invalid array index in property path
- Invalid list index in property path
AI-assisted analysis of spring-projects/spring-framework@69bf83ad71 (2026-08-09).
Data as JSON: /api/errors/db295b3b51b14fc3.
Report an issue: GitHub.
Appendix: 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 69bf83ad71)