spring-projects/spring-framework · error · InvalidPropertyException

Cannot set element with index {index} in List of size {size}

Error message

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

What it means

Thrown in the List branch when auto-growing a List to fill gaps before a high-index set. The loop calls list.add(null) for each gap; if the list rejects null (e.g. an unmodifiable list, an ImmutableList, or a list of non-null-conforming elements) add(null) throws NullPointerException and Spring rewraps it. The message explicitly notes the list does not support filling gaps with null elements.

Source

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

		}

		else if (propValue instanceof List list) {
			TypeDescriptor requiredType = ph.getCollectionType(tokens.keys.length);
			int index = Integer.parseInt(lastKey);
			Object oldValue = null;
			if (isExtractOldValueForEditor() && index < list.size()) {
				oldValue = list.get(index);
			}
			Object convertedValue = convertIfNecessary(tokens.canonicalName, oldValue, pv.getValue(),
					requiredType.getResolvableType().resolve(), requiredType);
			int size = list.size();
			if (index >= size && index < getAutoGrowCollectionLimit()) {
				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);
				}
			}
		}

View on GitHub (pinned to e8729d0438)

Solutions

  1. Initialize the list property with a mutable, null-permitting implementation (new ArrayList<>()) before binding.
  2. Lower the target index so no gaps need filling, or bind indices contiguously.
  3. Disable autoGrowNestedPaths and manage list growth yourself.
  4. Replace the null-hostile collection type with ArrayList (or LinkedList) on the bean.

Example fix

// before
public class Basket { List<Item> items = List.of(); } // immutable, no nulls
wrapper.setAutoGrowNestedPaths(true);
wrapper.setPropertyValue("items[3]", item);

// after
public class Basket { List<Item> items = new ArrayList<>(); }
Defensive patterns

Strategy: validation

Validate before calling

List<?> list = (List<?>) wrapper.getPropertyValue("items");
if (list != null && list.getClass().getName().startsWith("java.util.ArrayList")) {
    wrapper.setPropertyValue("items[5]", item);
} else {
    wrapper.setPropertyValue("items", new ArrayList<>(List.of()));
    wrapper.setPropertyValue("items[5]", item);
}

Type guard

static boolean allowsNullGaps(List<?> list) {
    try { ((List<Object>) list).add(null); list.remove(list.size() - 1); return true; }
    catch (Exception ex) { return false; }
}

Try / catch

try { wrapper.setPropertyValue("items[5]", item); }
catch (InvalidPropertyException ex) { /* replace with ArrayList then retry */ }

Prevention

When it happens

Trigger: Binding 'items[5]' onto a List initialized with Lists.newArrayList() that forbids nulls (Guava ImmutableList, Collections.checkedList of a primitive wrapper, a List backed by an array of a non-nullable type); autoGrowNestedPaths=true with a high index on a list that cannot hold nulls.

Common situations: Using immutable/null-hostile collections as bean properties; Kotlin/Java list types that reject null; auto-grow enabled on DTOs whose collections were pre-populated from immutable sources.

Related errors


AI-assisted analysis of spring-projects/spring-framework@e8729d0438 (2026-08-04). Data as JSON: /data/errors/4d803ff2d93de01b.json. Report an issue: GitHub.