{"id":"db295b3b51b14fc3","repo":"spring-projects/spring-framework","slug":"cannot-get-element-with-index-index-from-collect","errorCode":null,"errorMessage":"Cannot get element with index {index} from Collection of size {coll.size()}, accessed using property path '{propertyName}'","messagePattern":"Cannot get element with index (.+?) from Collection of size (.+?), accessed using property path '(.+?)'","errorType":"exception","errorClass":"InvalidPropertyException","httpStatus":null,"severity":"error","filePath":"spring-beans/src/main/java/org/springframework/beans/AbstractNestablePropertyAccessor.java","lineNumber":650,"sourceCode":"\t\t\t\t\telse if (value instanceof List list) {\n\t\t\t\t\t\tint index = Integer.parseInt(key);\n\t\t\t\t\t\tgrowCollectionIfNecessary(list, index, indexedPropertyName.toString(), ph, i + 1);\n\t\t\t\t\t\tvalue = list.get(index);\n\t\t\t\t\t}\n\t\t\t\t\telse if (value instanceof Map map) {\n\t\t\t\t\t\tClass<?> mapKeyType = ph.getResolvableType().getNested(i + 1).asMap().resolveGeneric(0);\n\t\t\t\t\t\t// IMPORTANT: Do not pass full property name in here - property editors\n\t\t\t\t\t\t// must not kick in for map keys but rather only for map values.\n\t\t\t\t\t\tTypeDescriptor typeDescriptor = TypeDescriptor.valueOf(mapKeyType);\n\t\t\t\t\t\tObject convertedMapKey = convertIfNecessary(null, null, key, mapKeyType, typeDescriptor);\n\t\t\t\t\t\tvalue = map.get(convertedMapKey);\n\t\t\t\t\t}\n\t\t\t\t\telse if (value instanceof Iterable iterable) {\n\t\t\t\t\t\t// Apply index to Iterator in case of a Set/Collection/Iterable.\n\t\t\t\t\t\tint index = Integer.parseInt(key);\n\t\t\t\t\t\tif (value instanceof Collection<?> coll) {\n\t\t\t\t\t\t\tif (index < 0 || index >= coll.size()) {\n\t\t\t\t\t\t\t\tthrow new InvalidPropertyException(getRootClass(), this.nestedPath + propertyName,\n\t\t\t\t\t\t\t\t\t\t\"Cannot get element with index \" + index + \" from Collection of size \" +\n\t\t\t\t\t\t\t\t\t\t\t\tcoll.size() + \", accessed using property path '\" + propertyName + \"'\");\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t\tIterator<Object> it = iterable.iterator();\n\t\t\t\t\t\tboolean found = false;\n\t\t\t\t\t\tint currIndex = 0;\n\t\t\t\t\t\tfor (; it.hasNext(); currIndex++) {\n\t\t\t\t\t\t\tObject elem = it.next();\n\t\t\t\t\t\t\tif (currIndex == index) {\n\t\t\t\t\t\t\t\tvalue = elem;\n\t\t\t\t\t\t\t\tfound = true;\n\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (!found) {\n\t\t\t\t\t\t\tthrow new InvalidPropertyException(getRootClass(), this.nestedPath + propertyName,\n\t\t\t\t\t\t\t\t\t\"Cannot get element with index \" + index + \" from Iterable of size \" +","sourceCodeStart":632,"sourceCodeEnd":668,"githubUrl":"https://github.com/spring-projects/spring-framework/blob/e8729d043887bf0d0baf91e062e909b56eb2b708/spring-beans/src/main/java/org/springframework/beans/AbstractNestablePropertyAccessor.java#L632-L668","documentation":"Raised in the Iterable/Collection branch of getPropertyValue(PropertyTokenHolder) when the value is a Collection and the requested index is < 0 or >= coll.size(). Spring throws InvalidPropertyException naming the index, the size, and the path. Unlike List, a generic Collection has no positional get(), so an out-of-range index is rejected up front before iterating.","triggerScenarios":"getProperty(\"set[3]\") on a Set of size 2; binding 'tags[10]' onto a Set field; reading an indexed value from a Collection that has fewer elements than the index.","commonSituations":"Using indexed access (brackets) on a Set; binding positional data to unordered collections; misreading a Set as if it were a List.","solutions":["Use a List instead of a Set if positional/indexed access is required.","Check the collection size before attempting indexed access.","Access Set members by iteration rather than by index."],"exampleFix":"// before\npublic class Doc { private Set<String> tags = new HashSet<>(); ... }\nObject t = wrapper.getPropertyValue(\"tags[1]\");\n\n// after\npublic class Doc { private List<String> tags = new ArrayList<>(); ... }","handlingStrategy":"validation","validationCode":"Object coll = wrapper.getPropertyValue(propertyName.replaceAll(\"\\\\[.*$\", \"\"));\nint idx = Integer.parseInt(propertyName.replaceAll(\".*\\\\[|\\\\].*\", \"\"));\nif (coll instanceof Collection<?> c && idx >= 0 && idx < c.size()) {\n    wrapper.getPropertyValue(propertyName);\n}","typeGuard":"static boolean indexInCollection(Collection<?> c, int idx) { return idx >= 0 && idx < c.size(); }","tryCatchPattern":"try { Object v = wrapper.getPropertyValue(propertyName); }\ncatch (InvalidPropertyException ex) { v = null; }","preventionTips":["Avoid bracket access on Set; use List for positional reads.","Bound-check indices against collection size before indexed access."],"tags":["spring-beans","bean-wrapper","collection","set","indexed-property","index-out-of-bounds"],"analyzedSha":"e8729d043887bf0d0baf91e062e909b56eb2b708","analyzedAt":"2026-08-04T19:07:39.725Z","schemaVersion":2}