spring-projects/spring-framework · error · InvalidPropertyException
Cannot get element with index
Error message
Cannot get element with index {index} from Iterable of size {currIndex}, accessed using property path '{propertyName}' What it means
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).
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.
Example fix
// before
private Iterable<String> rows = () -> Stream.of("a").iterator();
wrapper.getPropertyValue("rows[3]");
// after
private List<String> rows = new ArrayList<>(List.of("a"));
// ensure size >= 4 before rows[3] Defensive patterns
Strategy: validation
Validate before calling
Object raw = wrapper.getPropertyValue("rows");
if (raw instanceof Iterable<?> it) {
int seen = 0; Iterator<?> i = it.iterator();
while (i.hasNext()) { i.next(); seen++; if (seen > idx) break; }
if (seen <= idx) throw new IndexOutOfBoundsException("iterable has " + seen);
}
wrapper.getPropertyValue("rows[" + idx + "]"); Type guard
static boolean iterableLargeEnough(Iterable<?> it, int idx) {
int n = 0; for (Object ignored : it) if (++n > idx) return true; return false;
} Try / catch
try {
return wrapper.getPropertyValue("rows[" + idx + "]");
} catch (InvalidPropertyException ex) {
if (ex.getMessage().contains("from Iterable of size")) return null;
throw ex;
} Prevention
- 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.
When it happens
Trigger: getPropertyValue("stream[5]") on an Iterable with fewer than 6 elements (e.g. a Stream-backed iterable, a custom Iterable).
Common situations: 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.
Related errors
- Cannot get element with index
- Invalid property ' ' of bean class [ ]: Index of out of…
- Cannot access indexed value of property referenced in…
- Invalid array index in property path
- Invalid list index in property path
AI-assisted analysis of spring-projects/spring-framework@69bf83ad71 (2026-08-09).
Data as JSON: /api/errors/f978ed3313064555.
Report an issue: GitHub.
Appendix: source
Thrown at spring-beans/src/main/java/org/springframework/beans/AbstractNestablePropertyAccessor.java:667
if (index < 0 || index >= coll.size()) {
throw new InvalidPropertyException(getRootClass(), this.nestedPath + propertyName,
"Cannot get element with index " + index + " from Collection of size " +
coll.size() + ", accessed using property path '" + propertyName + "'");
}
}
Iterator<Object> it = iterable.iterator();
boolean found = false;
int currIndex = 0;
for (; it.hasNext(); currIndex++) {
Object elem = it.next();
if (currIndex == index) {
value = elem;
found = true;
break;
}
}
if (!found) {
throw new InvalidPropertyException(getRootClass(), this.nestedPath + propertyName,
"Cannot get element with index " + index + " from Iterable of size " +
currIndex + ", accessed using property path '" + propertyName + "'");
}
}
else {
throw new InvalidPropertyException(getRootClass(), this.nestedPath + propertyName,
"Property referenced in indexed property path '" + propertyName +
"' is neither an array nor a List/Set/Collection/Iterable nor a Map; " +
"returned value was [" + value + "]");
}
indexedPropertyName.append(PROPERTY_KEY_PREFIX).append(key).append(PROPERTY_KEY_SUFFIX);
}
}
return value;
}
catch (InvalidPropertyException ex) {
throw ex;
}View on GitHub (pinned to 69bf83ad71)