{"record":{"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/69bf83ad716d0cfc4b0520a19b4d8b24c79d1538/spring-beans/src/main/java/org/springframework/beans/AbstractNestablePropertyAccessor.java#L632-L668","documentation":"Thrown by getPropertyValue in the Collection branch (Set/Collection indexed read) when the requested index is < 0 or >= coll.size(). Sets and generic Collections have no positional access, so Spring iterates; before iterating it bounds-checks the size against the index for a clear error.","triggerScenarios":"getPropertyValue(\"set[3]\") on a Set/Collection of size <= 3 (or negative index).","commonSituations":"Binding/reading indexed positions on a Set (order may be undefined); client expecting list semantics on a Set field; index derived from request params that exceeds collection size.","solutions":["Change the field to a List if positional access is required (List preserves insertion order and supports get).","Bounds-check the index against collection.size() before issuing the indexed read.","If the Set is intentional, expose iteration rather than positional access in the API.","Reject/normalize out-of-range indices in the controller or validator."],"exampleFix":"// before\nprivate Set<String> tags = new HashSet<>(); // size 1\nwrapper.getPropertyValue(\"tags[3]\");\n\n// after\nprivate List<String> tags = new ArrayList<>();\nwrapper.getPropertyValue(\"tags[3]\"); // with size >= 4","handlingStrategy":"validation","validationCode":"BeanWrapper w = new BeanWrapperImpl(bean);\nObject coll = w.getPropertyValue(\"tags\");\nif (coll instanceof Collection<?> c && (idx < 0 || idx >= c.size())) {\n    throw new IndexOutOfBoundsException(\"set has \" + c.size() + \" elements; requested \" + idx);\n}\nw.getPropertyValue(\"tags[\" + idx + \"]\");","typeGuard":"static boolean indexInCollection(Object coll, int idx) {\n    return coll instanceof Collection<?> c && idx >= 0 && idx < c.size();\n}","tryCatchPattern":"try {\n    return wrapper.getPropertyValue(\"tags[\" + idx + \"]\");\n} catch (InvalidPropertyException ex) {\n    if (ex.getMessage().contains(\"from Collection of size\")) return null; // out of range\n    throw ex;\n}","preventionTips":["Use a List instead of a Set whenever positional access is required.","Bounds-check indices against collection.size() before indexed reads.","Document that Set ordering is iteration-defined, not index-stable."],"tags":["spring-beans","bean-wrapper","collection","set","index-out-of-bounds","read"],"backgroundTag":null,"analyzedSha":"69bf83ad716d0cfc4b0520a19b4d8b24c79d1538","analyzedAt":"2026-08-09T15:32:58.770Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}