{"record":{"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/69bf83ad716d0cfc4b0520a19b4d8b24c79d1538/spring-beans/src/main/java/org/springframework/beans/AbstractNestablePropertyAccessor.java#L298-L334","documentation":"Thrown while auto-growing a List to reach a target index: the loop calls list.add(null) to fill the gap, and if that throws NullPointerException (list rejects null), Spring wraps it as InvalidPropertyException. Happens only when index >= size and index < autoGrowCollectionLimit, i.e. the gap-fill path.","triggerScenarios":"setProperty(\"list[5]\", value) where 'list' is an instance that forbids null elements (e.g. Collections.checkedList with a primitive-wrapper check, a Guava ImmutableList/ImmutableList.copyOf, a list backed by ConcurrentHashMap, or Collections.singletonList), and autoGrow needs to pad with nulls.","commonSituations":"Target bean exposes an immutable or null-hostile List; binding sparse indices onto such a list; the bean initializes its List field with List.of(...) or Immutable* types.","solutions":["Initialize the List field with a mutable, null-accepting type such as new ArrayList<>().","Avoid sparse indices — submit sequential indices so no gap-fill is required.","Disable autoGrow and pre-populate the list with placeholders to the required size.","If the list genuinely cannot hold nulls, switch the field to a Map<Integer, T>."],"exampleFix":"// before\npublic List<String> items = List.of(); // immutable, rejects null + add\n\n// after\npublic List<String> items = new ArrayList<>();","handlingStrategy":"validation","validationCode":"BeanWrapper w = new BeanWrapperImpl(bean);\nObject list = w.getPropertyValue(\"items\");\nif (list instanceof List<?> l) {\n    try { l.add(null); l.remove(l.size() - 1); } // probe null support\n    catch (NullPointerException npe) {\n        throw new IllegalStateException(\"target List rejects null; cannot autogrow\", npe);\n    }\n}\nw.setPropertyValue(\"items[\" + idx + \"]\", value);","typeGuard":"static boolean listAcceptsNullAndGrows(List<?> list) {\n    if (list instanceof java.util.concurrent.CopyOnWriteArrayList) return false;\n    try { boolean r = list.add(null); list.remove(list.size() - 1); return r; }\n    catch (Throwable t) { return false; }\n}","tryCatchPattern":"try {\n    wrapper.setPropertyValue(\"items[\" + idx + \"]\", v);\n} catch (InvalidPropertyException ex) {\n    if (ex.getMessage().contains(\"filling up gaps\")) { /* replace immutable/null-hostile list with ArrayList */ }\n    else throw ex;\n}","preventionTips":["Initialize List fields with new ArrayList<>() — mutable and null-tolerant.","Avoid List.of / Guava ImmutableList / unmodifiable wrappers for bindable fields.","Submit sequential indices so no gap-fill is needed."],"tags":["spring-beans","bean-wrapper","list","autogrow","null-elements"],"backgroundTag":null,"analyzedSha":"69bf83ad716d0cfc4b0520a19b4d8b24c79d1538","analyzedAt":"2026-08-09T15:32:58.770Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}