spring-projects/spring-framework · error · InvalidPropertyException

Invalid list index in property path

Error message

Invalid list index in property path '{tokens.canonicalName}'

What it means

Thrown by processKeyedProperty in the List branch when list.set(index, convertedValue) raises IndexOutOfBoundsException — the non-autogrow path (index < size fails and index >= autoGrowCollectionLimit, or autoGrow off). Spring wraps it as InvalidPropertyException with the canonical property name.

Solutions

  1. Enable setAutoGrowNestedPaths(true) (and check autoGrowCollectionLimit) so the list extends to the index.
  2. Pre-populate the list to at least index+1 elements before binding.
  3. Validate the index against list.size() in a custom binder and reject the request if out of range.
  4. Use a Map<Integer, T> for sparse/arbitrary indices.

Example fix

// before
List<String> items = new ArrayList<>(); // size 0
wrapper.setPropertyValue("items[2]", v); // IndexOutOfBounds

// after
wrapper.setAutoGrowNestedPaths(true);
wrapper.setPropertyValue("items[2]", v);
Defensive patterns

Strategy: validation

Validate before calling

BeanWrapper w = new BeanWrapperImpl(bean);
Object base = w.getPropertyValue("items");
if (base instanceof List<?> l && (idx < 0 || idx >= l.size())) {
    w.setAutoGrowNestedPaths(true);
}
w.setPropertyValue("items[" + idx + "]", value);

Type guard

static boolean inListBounds(Object list, int idx) {
    return list instanceof List<?> l && idx >= 0 && idx < l.size();
}

Try / catch

try {
    wrapper.setPropertyValue("items[" + idx + "]", v);
} catch (InvalidPropertyException ex) {
    if (ex.getCause() instanceof IndexOutOfBoundsException) { /* pre-size list or enable autogrow */ }
    else throw ex;
}

Prevention

When it happens

Trigger: setProperty("list[5]", value) with autoGrow disabled (or index beyond the limit) on a list of size <= 5; i.e. trying to set an index that does not yet exist.

Common situations: autoGrowNestedPaths not set; binding a zero-based sequential form onto a freshly constructed empty list; binding indices beyond autoGrowCollectionLimit.

Related errors


AI-assisted analysis of spring-projects/spring-framework@69bf83ad71 (2026-08-09). Data as JSON: /api/errors/b2d3c4ecf5852483. Report an issue: GitHub.

Appendix: source

Thrown at spring-beans/src/main/java/org/springframework/beans/AbstractNestablePropertyAccessor.java:329

				for (int i = size; i < index; i++) {
					try {
						list.add(null);
					}
					catch (NullPointerException ex) {
						throw new InvalidPropertyException(getRootClass(), this.nestedPath + tokens.canonicalName,
								"Cannot set element with index " + index + " in List of size " +
								size + ", accessed using property path '" + tokens.canonicalName +
								"': List does not support filling up gaps with null elements");
					}
				}
				list.add(convertedValue);
			}
			else {
				try {
					list.set(index, convertedValue);
				}
				catch (IndexOutOfBoundsException ex) {
					throw new InvalidPropertyException(getRootClass(), this.nestedPath + tokens.canonicalName,
							"Invalid list index in property path '" + tokens.canonicalName + "'", ex);
				}
			}
		}

		else if (propValue instanceof Map map) {
			TypeDescriptor mapKeyType = ph.getMapKeyType(tokens.keys.length);
			TypeDescriptor mapValueType = ph.getMapValueType(tokens.keys.length);
			// IMPORTANT: Do not pass full property name in here - property editors
			// must not kick in for map keys but rather only for map values.
			Object convertedMapKey = convertIfNecessary(null, null, lastKey,
					mapKeyType.getResolvableType().resolve(), mapKeyType);
			Object oldValue = null;
			if (isExtractOldValueForEditor()) {
				oldValue = map.get(convertedMapKey);
			}
			// Pass full property name and old value in here, since we want full
			// conversion ability for map values.

View on GitHub (pinned to 69bf83ad71)