spring-projects/spring-framework · error · InvalidPropertyException

Cannot set element with index

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 while auto-growing a List to reach a target index: the loop calls list.add(null) to fill the gap, and if that throws NullPointerException (list rejects null), Spring wraps it as InvalidPropertyException. Happens only when index >= size and index < autoGrowCollectionLimit, i.e. the gap-fill path.

Solutions

  1. Initialize the List field with a mutable, null-accepting type such as new ArrayList<>().
  2. Avoid sparse indices — submit sequential indices so no gap-fill is required.
  3. Disable autoGrow and pre-populate the list with placeholders to the required size.
  4. If the list genuinely cannot hold nulls, switch the field to a Map<Integer, T>.

Example fix

// before
public List<String> items = List.of(); // immutable, rejects null + add

// after
public List<String> items = new ArrayList<>();
Defensive patterns

Strategy: validation

Validate before calling

BeanWrapper w = new BeanWrapperImpl(bean);
Object list = w.getPropertyValue("items");
if (list instanceof List<?> l) {
    try { l.add(null); l.remove(l.size() - 1); } // probe null support
    catch (NullPointerException npe) {
        throw new IllegalStateException("target List rejects null; cannot autogrow", npe);
    }
}
w.setPropertyValue("items[" + idx + "]", value);

Type guard

static boolean listAcceptsNullAndGrows(List<?> list) {
    if (list instanceof java.util.concurrent.CopyOnWriteArrayList) return false;
    try { boolean r = list.add(null); list.remove(list.size() - 1); return r; }
    catch (Throwable t) { return false; }
}

Try / catch

try {
    wrapper.setPropertyValue("items[" + idx + "]", v);
} catch (InvalidPropertyException ex) {
    if (ex.getMessage().contains("filling up gaps")) { /* replace immutable/null-hostile list with ArrayList */ }
    else throw ex;
}

Prevention

When it happens

Trigger: setProperty("list[5]", value) where 'list' is an instance that forbids null elements (e.g. Collections.checkedList with a primitive-wrapper check, a Guava ImmutableList/ImmutableList.copyOf, a list backed by ConcurrentHashMap, or Collections.singletonList), and autoGrow needs to pad with nulls.

Common situations: Target bean exposes an immutable or null-hostile List; binding sparse indices onto such a list; the bean initializes its List field with List.of(...) or Immutable* types.

Related errors


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

Appendix: 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 69bf83ad71)