{"id":"4d803ff2d93de01b","repo":"spring-projects/spring-framework","slug":"cannot-set-element-with-index-index-in-list-of-s","errorCode":null,"errorMessage":"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","messagePattern":"Cannot set element with index (.+?) in List of size (.+?), accessed using property path '(.+?)': List does not support filling up gaps with null elements","errorType":"exception","errorClass":"InvalidPropertyException","httpStatus":null,"severity":"error","filePath":"spring-beans/src/main/java/org/springframework/beans/AbstractNestablePropertyAccessor.java","lineNumber":316,"sourceCode":"\t\t}\n\n\t\telse if (propValue instanceof List list) {\n\t\t\tTypeDescriptor requiredType = ph.getCollectionType(tokens.keys.length);\n\t\t\tint index = Integer.parseInt(lastKey);\n\t\t\tObject oldValue = null;\n\t\t\tif (isExtractOldValueForEditor() && index < list.size()) {\n\t\t\t\toldValue = list.get(index);\n\t\t\t}\n\t\t\tObject convertedValue = convertIfNecessary(tokens.canonicalName, oldValue, pv.getValue(),\n\t\t\t\t\trequiredType.getResolvableType().resolve(), requiredType);\n\t\t\tint size = list.size();\n\t\t\tif (index >= size && index < getAutoGrowCollectionLimit()) {\n\t\t\t\tfor (int i = size; i < index; i++) {\n\t\t\t\t\ttry {\n\t\t\t\t\t\tlist.add(null);\n\t\t\t\t\t}\n\t\t\t\t\tcatch (NullPointerException ex) {\n\t\t\t\t\t\tthrow new InvalidPropertyException(getRootClass(), this.nestedPath + tokens.canonicalName,\n\t\t\t\t\t\t\t\t\"Cannot set element with index \" + index + \" in List of size \" +\n\t\t\t\t\t\t\t\tsize + \", accessed using property path '\" + tokens.canonicalName +\n\t\t\t\t\t\t\t\t\"': List does not support filling up gaps with null elements\");\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tlist.add(convertedValue);\n\t\t\t}\n\t\t\telse {\n\t\t\t\ttry {\n\t\t\t\t\tlist.set(index, convertedValue);\n\t\t\t\t}\n\t\t\t\tcatch (IndexOutOfBoundsException ex) {\n\t\t\t\t\tthrow new InvalidPropertyException(getRootClass(), this.nestedPath + tokens.canonicalName,\n\t\t\t\t\t\t\t\"Invalid list index in property path '\" + tokens.canonicalName + \"'\", ex);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n","sourceCodeStart":298,"sourceCodeEnd":334,"githubUrl":"https://github.com/spring-projects/spring-framework/blob/e8729d043887bf0d0baf91e062e909b56eb2b708/spring-beans/src/main/java/org/springframework/beans/AbstractNestablePropertyAccessor.java#L298-L334","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\npublic class Basket { List<Item> items = List.of(); } // immutable, no nulls\nwrapper.setAutoGrowNestedPaths(true);\nwrapper.setPropertyValue(\"items[3]\", item);\n\n// after\npublic class Basket { List<Item> items = new ArrayList<>(); }","handlingStrategy":"validation","validationCode":"List<?> list = (List<?>) wrapper.getPropertyValue(\"items\");\nif (list != null && list.getClass().getName().startsWith(\"java.util.ArrayList\")) {\n    wrapper.setPropertyValue(\"items[5]\", item);\n} else {\n    wrapper.setPropertyValue(\"items\", new ArrayList<>(List.of()));\n    wrapper.setPropertyValue(\"items[5]\", item);\n}","typeGuard":"static boolean allowsNullGaps(List<?> list) {\n    try { ((List<Object>) list).add(null); list.remove(list.size() - 1); return true; }\n    catch (Exception ex) { return false; }\n}","tryCatchPattern":"try { wrapper.setPropertyValue(\"items[5]\", item); }\ncatch (InvalidPropertyException ex) { /* replace with ArrayList then retry */ }","preventionTips":["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."],"tags":["spring-beans","bean-wrapper","list","auto-grow","null-elements","indexed-property"],"analyzedSha":"e8729d043887bf0d0baf91e062e909b56eb2b708","analyzedAt":"2026-08-04T19:07:39.725Z","schemaVersion":2}