{"record":{"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/69bf83ad716d0cfc4b0520a19b4d8b24c79d1538/spring-beans/src/main/java/org/springframework/beans/AbstractNestablePropertyAccessor.java#L649-L685","documentation":"Thrown by getPropertyValue in the Iterable branch after iterating the whole iterable without reaching the target index. Unlike the Collection case there is no size() available up front, so Spring walks the iterator and, if it runs out before 'index', raises InvalidPropertyException reporting the number of elements seen (currIndex).","triggerScenarios":"getPropertyValue(\"stream[5]\") on an Iterable with fewer than 6 elements (e.g. a Stream-backed iterable, a custom Iterable).","commonSituations":"Reading indexed positions on a custom Iterable that does not implement Collection; an Iterable that is empty or smaller than expected; assuming list semantics on a non-collection iterable.","solutions":["Materialize the Iterable into a List before indexed access: List<T> list = StreamSupport.stream(...).toList().","Verify the index against the actual element count before reading positionally.","Expose a List-typed property instead of a raw Iterable for positional binding.","Handle the InvalidPropertyException when the iterable is legitimately shorter and fall back to a default."],"exampleFix":"// before\nprivate Iterable<String> rows = () -> Stream.of(\"a\").iterator();\nwrapper.getPropertyValue(\"rows[3]\");\n\n// after\nprivate List<String> rows = new ArrayList<>(List.of(\"a\"));\n// ensure size >= 4 before rows[3]","handlingStrategy":"validation","validationCode":"Object raw = wrapper.getPropertyValue(\"rows\");\nif (raw instanceof Iterable<?> it) {\n    int seen = 0; Iterator<?> i = it.iterator();\n    while (i.hasNext()) { i.next(); seen++; if (seen > idx) break; }\n    if (seen <= idx) throw new IndexOutOfBoundsException(\"iterable has \" + seen);\n}\nwrapper.getPropertyValue(\"rows[\" + idx + \"]\");","typeGuard":"static boolean iterableLargeEnough(Iterable<?> it, int idx) {\n    int n = 0; for (Object ignored : it) if (++n > idx) return true; return false;\n}","tryCatchPattern":"try {\n    return wrapper.getPropertyValue(\"rows[\" + idx + \"]\");\n} catch (InvalidPropertyException ex) {\n    if (ex.getMessage().contains(\"from Iterable of size\")) return null;\n    throw ex;\n}","preventionTips":["Materialize Iterables into Lists before positional access.","Expose List-typed properties for index-based binding.","Avoid relying on index access for Streams or custom Iterables."],"tags":["spring-beans","bean-wrapper","iterable","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"}