{"id":"f978ed3313064555","repo":"spring-projects/spring-framework","slug":"cannot-get-element-with-index-index-from-iterabl","errorCode":null,"errorMessage":"Cannot get element with index {index} from Iterable of size {currIndex}, accessed using property path '{propertyName}'","messagePattern":"Cannot get element with index (.+?) from Iterable 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":667,"sourceCode":"\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 \" +\n\t\t\t\t\t\t\t\t\t\t\tcurrIndex + \", accessed using property path '\" + propertyName + \"'\");\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\telse {\n\t\t\t\t\t\tthrow new InvalidPropertyException(getRootClass(), this.nestedPath + propertyName,\n\t\t\t\t\t\t\t\t\"Property referenced in indexed property path '\" + propertyName +\n\t\t\t\t\t\t\t\t\t\t\"' is neither an array nor a List/Set/Collection/Iterable nor a Map; \" +\n\t\t\t\t\t\t\t\t\t\t\"returned value was [\" + value + \"]\");\n\t\t\t\t\t}\n\t\t\t\t\tindexedPropertyName.append(PROPERTY_KEY_PREFIX).append(key).append(PROPERTY_KEY_SUFFIX);\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn value;\n\t\t}\n\t\tcatch (InvalidPropertyException ex) {\n\t\t\tthrow ex;\n\t\t}","sourceCodeStart":649,"sourceCodeEnd":685,"githubUrl":"https://github.com/spring-projects/spring-framework/blob/e8729d043887bf0d0baf91e062e909b56eb2b708/spring-beans/src/main/java/org/springframework/beans/AbstractNestablePropertyAccessor.java#L649-L685","documentation":"Raised in the Iterable branch when the value is an Iterable (but not a Collection, so size is unknown up front). Spring iterates looking for the element at the requested index; if the iterator is exhausted without finding it, InvalidPropertyException is thrown with 'Iterable of size {currIndex}'. currIndex reflects how many elements were actually seen.","triggerScenarios":"getProperty(\"stream[5]\") on an Iterable backed by a Stream or custom iterator with fewer than 6 elements; reading 'items[2]' from a Queue/deque-like Iterable with only 1 element; binding an index to a non-Collection Iterable.","commonSituations":"Custom Iterable types, Queue/Deque fields accessed by index; Stream-backed lazy iterables; misusing indexed access on non-List iterables.","solutions":["Use a List (or copy the Iterable into a List) when you need indexed access.","Verify the Iterable contains enough elements before indexed access.","Avoid bracket syntax on Stream-backed or lazy iterables."],"exampleFix":"// before\npublic class Batch { private Iterable<Row> rows; ... } // unknown size\nObject r = wrapper.getPropertyValue(\"rows[3]\");\n\n// after\npublic class Batch { private List<Row> rows = new ArrayList<>(); ... }","handlingStrategy":"validation","validationCode":"Object it = wrapper.getPropertyValue(propertyName.replaceAll(\"\\\\[.*$\", \"\"));\nint idx = Integer.parseInt(propertyName.replaceAll(\".*\\\\[|\\\\].*\", \"\"));\nif (it instanceof Collection<?> c && idx >= 0 && idx < c.size()) {\n    wrapper.getPropertyValue(propertyName);\n} else if (!(it instanceof Iterable)) {\n    throw new IllegalArgumentException(\"not iterable\");\n}","typeGuard":"static boolean safeIterableIndex(Object value, int idx) {\n    if (value instanceof Collection<?> c) return idx >= 0 && idx < c.size();\n    return false; // non-Collection Iterable: cannot bound-check up front\n}","tryCatchPattern":"try { Object v = wrapper.getPropertyValue(propertyName); }\ncatch (InvalidPropertyException ex) { v = null; }","preventionTips":["Copy custom Iterables into Lists before indexed access.","Avoid bracket syntax on Stream-backed or lazy Iterables.","Prefer List types on bean fields you intend to index."],"tags":["spring-beans","bean-wrapper","iterable","indexed-property","index-out-of-bounds"],"analyzedSha":"e8729d043887bf0d0baf91e062e909b56eb2b708","analyzedAt":"2026-08-04T19:07:39.725Z","schemaVersion":2}