spring-projects/spring-framework · error · InvalidPropertyException
Invalid list index in property path '{tokens.canonicalName}'
Error message
Invalid list index in property path '{tokens.canonicalName}' What it means
Thrown in the List branch of processKeyedProperty when the index is in range that does not trigger auto-grow (index < size expected) and list.set(index, convertedValue) throws IndexOutOfBoundsException. Spring rewraps it as InvalidPropertyException 'Invalid list index'. This is the non-auto-grow / contiguous-range failure for indexed List writes.
Source
Thrown at spring-beans/src/main/java/org/springframework/beans/AbstractNestablePropertyAccessor.java:329
for (int i = size; i < index; i++) {
try {
list.add(null);
}
catch (NullPointerException ex) {
throw new InvalidPropertyException(getRootClass(), this.nestedPath + tokens.canonicalName,
"Cannot set element with index " + index + " in List of size " +
size + ", accessed using property path '" + tokens.canonicalName +
"': List does not support filling up gaps with null elements");
}
}
list.add(convertedValue);
}
else {
try {
list.set(index, convertedValue);
}
catch (IndexOutOfBoundsException ex) {
throw new InvalidPropertyException(getRootClass(), this.nestedPath + tokens.canonicalName,
"Invalid list index in property path '" + tokens.canonicalName + "'", ex);
}
}
}
else if (propValue instanceof Map map) {
TypeDescriptor mapKeyType = ph.getMapKeyType(tokens.keys.length);
TypeDescriptor mapValueType = ph.getMapValueType(tokens.keys.length);
// IMPORTANT: Do not pass full property name in here - property editors
// must not kick in for map keys but rather only for map values.
Object convertedMapKey = convertIfNecessary(null, null, lastKey,
mapKeyType.getResolvableType().resolve(), mapKeyType);
Object oldValue = null;
if (isExtractOldValueForEditor()) {
oldValue = map.get(convertedMapKey);
}
// Pass full property name and old value in here, since we want full
// conversion ability for map values.View on GitHub (pinned to e8729d0438)
Solutions
- Enable wrapper.setAutoGrowNestedPaths(true) so the list grows up to the auto-grow limit.
- Pre-fill the list so the target index already exists before binding.
- Use contiguous indices starting at 0 when binding sequentially.
- Switch to add-style binding via a non-indexed property if order is not significant.
Example fix
// before
List<Item> items = new ArrayList<>(); // size 0
wrapper.setPropertyValue("items[2]", item); // autoGrow off -> OOB
// after
wrapper.setAutoGrowNestedPaths(true);
wrapper.setPropertyValue("items[2]", item); Defensive patterns
Strategy: validation
Validate before calling
int idx = 7;
List<Object> list = (List<Object>) wrapper.getPropertyValue("items");
if (list == null) { list = new ArrayList<>(); wrapper.setPropertyValue("items", list); }
while (list.size() <= idx) list.add(null);
wrapper.setPropertyValue("items[" + idx + "]", item); Type guard
static boolean indexExistsOrGrow(List<?> list, BeanWrapper bw, int idx) {
return idx < list.size() || (bw.isAutoGrowNestedPaths() && idx < bw.getAutoGrowCollectionLimit());
} Try / catch
try { wrapper.setPropertyValue("items[" + idx + "]", item); }
catch (InvalidPropertyException ex) { /* pre-fill list then retry */ } Prevention
- Enable autoGrowNestedPaths for tolerant indexed writes.
- Pre-fill lists to the required size before binding.
- Use contiguous indices when binding sequentially.
When it happens
Trigger: setProperty("items[7]", x) on an ArrayList of size 3 with autoGrowNestedPaths=false; index beyond the list size but auto-grow off; binding to a list that was not pre-sized.
Common situations: autoGrowNestedPaths disabled by default in custom BeanWrapper usage; binding positional data into a List without pre-filling; converting from array semantics where auto-grow was expected but not enabled.
Related errors
- Invalid array index in property path '{tokens.canonicalName}
- Cannot set element with index {index} in List of size {size}
- Cannot get element with index {index} from Collection of siz
- Cannot get element with index {index} from Iterable of size
- Index of out of bounds in property path '${propertyName}'
AI-assisted analysis of spring-projects/spring-framework@e8729d0438 (2026-08-04).
Data as JSON: /data/errors/b2d3c4ecf5852483.json.
Report an issue: GitHub.