{"record":{"id":"84494e4436ee035e","repo":"spring-projects/spring-framework","slug":"cannot-access-indexed-value-in-property-referenced-84494e","errorCode":null,"errorMessage":"Cannot access indexed value in property referenced in indexed property path '{tokens.canonicalName}': returned null","messagePattern":"Cannot access indexed value in property referenced in indexed property path '(.+?)': returned null","errorType":"exception","errorClass":"NullValueInNestedPathException","httpStatus":null,"severity":"error","filePath":"spring-beans/src/main/java/org/springframework/beans/AbstractNestablePropertyAccessor.java","lineNumber":386,"sourceCode":"\t\tObject propValue;\n\t\ttry {\n\t\t\tpropValue = getPropertyValue(getterTokens);\n\t\t}\n\t\tcatch (NotReadablePropertyException ex) {\n\t\t\tthrow new NotWritablePropertyException(getRootClass(), this.nestedPath + tokens.canonicalName,\n\t\t\t\t\t\"Cannot access indexed value in property referenced \" +\n\t\t\t\t\t\"in indexed property path '\" + tokens.canonicalName + \"'\", ex);\n\t\t}\n\n\t\tif (propValue == null) {\n\t\t\t// null map value case\n\t\t\tif (isAutoGrowNestedPaths()) {\n\t\t\t\tint lastKeyIndex = tokens.canonicalName.lastIndexOf('[');\n\t\t\t\tgetterTokens.canonicalName = tokens.canonicalName.substring(0, lastKeyIndex);\n\t\t\t\tpropValue = setDefaultValue(getterTokens);\n\t\t\t}\n\t\t\telse {\n\t\t\t\tthrow new NullValueInNestedPathException(getRootClass(), this.nestedPath + tokens.canonicalName,\n\t\t\t\t\t\t\"Cannot access indexed value in property referenced \" +\n\t\t\t\t\t\t\"in indexed property path '\" + tokens.canonicalName + \"': returned null\");\n\t\t\t}\n\t\t}\n\t\treturn propValue;\n\t}\n\n\tprivate void processLocalProperty(PropertyTokenHolder tokens, PropertyValue pv) {\n\t\tPropertyHandler ph = getLocalPropertyHandler(tokens.actualName);\n\t\tif (ph == null || !ph.isWritable()) {\n\t\t\tif (pv.isOptional()) {\n\t\t\t\tif (logger.isDebugEnabled()) {\n\t\t\t\t\tlogger.debug(\"Ignoring optional value for property '\" + tokens.actualName +\n\t\t\t\t\t\t\t\"' - property not found on bean class [\" + getRootClass().getName() + \"]\");\n\t\t\t\t}\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif (this.suppressNotWritablePropertyException) {","sourceCodeStart":368,"sourceCodeEnd":404,"githubUrl":"https://github.com/spring-projects/spring-framework/blob/69bf83ad716d0cfc4b0520a19b4d8b24c79d1538/spring-beans/src/main/java/org/springframework/beans/AbstractNestablePropertyAccessor.java#L368-L404","documentation":"Thrown by getPropertyHoldingValue when the holding value for a keyed write is null and setAutoGrowNestedPaths is false. Without auto-grow Spring cannot create the container, so it raises NullValueInNestedPathException naming the canonical path.","triggerScenarios":"setProperty(\"items[0]\", value) where getItems() returns null and autoGrowNestedPaths is disabled (the default in some contexts).","commonSituations":"Bean field left null with no initializer; binding into a freshly constructed bean whose collection/map fields are null; autoGrow turned off for safety; nested wrapper created without inheriting autoGrow settings.","solutions":["Initialize the collection/map field: e.g. private List<T> items = new ArrayList<>();.","Call wrapper.setAutoGrowNestedPaths(true) before the write (propagated to nested accessors).","Set the holding property to a non-null container before addressing an index/key.","Pre-flight check getPropertyValue(base) for null and instantiate explicitly."],"exampleFix":"// before\nprivate List<String> items; // null\nwrapper.setPropertyValue(\"items[0]\", \"a\");\n\n// after\nprivate List<String> items = new ArrayList<>();\n// or\nwrapper.setAutoGrowNestedPaths(true);","handlingStrategy":"validation","validationCode":"BeanWrapper w = new BeanWrapperImpl(bean);\nString base = path.substring(0, path.indexOf('['));\nObject holder = w.getPropertyValue(base);\nif (holder == null) {\n    // either initialize the field or enable autogrow\n    w.setAutoGrowNestedPaths(true);\n}\nw.setPropertyValue(path, value);","typeGuard":"static boolean holderNotNull(BeanWrapper w, String path) {\n    String base = path.substring(0, path.indexOf('['));\n    return w.getPropertyValue(base) != null;\n}","tryCatchPattern":"try {\n    wrapper.setPropertyValue(path, value);\n} catch (NullValueInNestedPathException ex) {\n    if (ex.getMessage().contains(\"returned null\")) { /* initialize container or enable autogrow */ }\n    else throw ex;\n}","preventionTips":["Initialize collection/map fields inline (private List<T> items = new ArrayList<>();).","Enable setAutoGrowNestedPaths(true) when binding onto beans with null containers.","Return empty containers from getters instead of null."],"tags":["spring-beans","bean-wrapper","indexed-property","null-value","autogrow"],"backgroundTag":null,"analyzedSha":"69bf83ad716d0cfc4b0520a19b4d8b24c79d1538","analyzedAt":"2026-08-09T15:32:58.770Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}