spring-projects/spring-framework · error · InvalidPropertyException
Cannot set element with index {index} in List of size {size}
Error message
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 What it means
Thrown in the List branch when auto-growing a List to fill gaps before a high-index set. The loop calls list.add(null) for each gap; if the list rejects null (e.g. an unmodifiable list, an ImmutableList, or a list of non-null-conforming elements) add(null) throws NullPointerException and Spring rewraps it. The message explicitly notes the list does not support filling gaps with null elements.
Source
Thrown at spring-beans/src/main/java/org/springframework/beans/AbstractNestablePropertyAccessor.java:316
}
else if (propValue instanceof List list) {
TypeDescriptor requiredType = ph.getCollectionType(tokens.keys.length);
int index = Integer.parseInt(lastKey);
Object oldValue = null;
if (isExtractOldValueForEditor() && index < list.size()) {
oldValue = list.get(index);
}
Object convertedValue = convertIfNecessary(tokens.canonicalName, oldValue, pv.getValue(),
requiredType.getResolvableType().resolve(), requiredType);
int size = list.size();
if (index >= size && index < getAutoGrowCollectionLimit()) {
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);
}
}
}
View on GitHub (pinned to e8729d0438)
Solutions
- Initialize the list property with a mutable, null-permitting implementation (new ArrayList<>()) before binding.
- Lower the target index so no gaps need filling, or bind indices contiguously.
- Disable autoGrowNestedPaths and manage list growth yourself.
- Replace the null-hostile collection type with ArrayList (or LinkedList) on the bean.
Example fix
// before
public class Basket { List<Item> items = List.of(); } // immutable, no nulls
wrapper.setAutoGrowNestedPaths(true);
wrapper.setPropertyValue("items[3]", item);
// after
public class Basket { List<Item> items = new ArrayList<>(); } Defensive patterns
Strategy: validation
Validate before calling
List<?> list = (List<?>) wrapper.getPropertyValue("items");
if (list != null && list.getClass().getName().startsWith("java.util.ArrayList")) {
wrapper.setPropertyValue("items[5]", item);
} else {
wrapper.setPropertyValue("items", new ArrayList<>(List.of()));
wrapper.setPropertyValue("items[5]", item);
} Type guard
static boolean allowsNullGaps(List<?> list) {
try { ((List<Object>) list).add(null); list.remove(list.size() - 1); return true; }
catch (Exception ex) { return false; }
} Try / catch
try { wrapper.setPropertyValue("items[5]", item); }
catch (InvalidPropertyException ex) { /* replace with ArrayList then retry */ } Prevention
- Initialize collection fields with mutable, null-permitting List implementations.
- Avoid ImmutableList / List.of() / null-hostile lists on bindable beans.
- Keep auto-grow indices contiguous to minimize null-gap filling.
When it happens
Trigger: Binding 'items[5]' onto a List initialized with Lists.newArrayList() that forbids nulls (Guava ImmutableList, Collections.checkedList of a primitive wrapper, a List backed by an array of a non-nullable type); autoGrowNestedPaths=true with a high index on a list that cannot hold nulls.
Common situations: Using immutable/null-hostile collections as bean properties; Kotlin/Java list types that reject null; auto-grow enabled on DTOs whose collections were pre-populated from immutable sources.
Related errors
- Invalid list index in property path '{tokens.canonicalName}'
- Cannot access indexed value in property referenced in indexe
- Cannot access indexed value of property referenced in indexe
- No property handler found
- Invalid array index in property path '{tokens.canonicalName}
AI-assisted analysis of spring-projects/spring-framework@e8729d0438 (2026-08-04).
Data as JSON: /data/errors/4d803ff2d93de01b.json.
Report an issue: GitHub.