spring-projects/spring-framework · error · NullValueInNestedPathException

Cannot access indexed value of property referenced in indexe

Error message

Cannot access indexed value of property referenced in indexed property path '{propertyName}': returned null

What it means

Raised at the start of the key-application loop in getPropertyValue(PropertyTokenHolder): the property handler is readable and ph.getValue() was obtained, but it is null and the path still has index/map keys to apply. With autoGrowNestedPaths off, Spring cannot dereference a null, so it throws NullValueInNestedPathException 'returned null'. The propertyName in the message is the full indexed path.

Source

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

	}

	@SuppressWarnings({"rawtypes", "unchecked"})
	protected @Nullable Object getPropertyValue(PropertyTokenHolder tokens) throws BeansException {
		String propertyName = tokens.canonicalName;
		String actualName = tokens.actualName;
		PropertyHandler ph = getLocalPropertyHandler(actualName);
		if (ph == null || !ph.isReadable()) {
			throw new NotReadablePropertyException(getRootClass(), this.nestedPath + propertyName);
		}
		try {
			Object value = ph.getValue();
			if (tokens.keys != null) {
				if (value == null) {
					if (isAutoGrowNestedPaths()) {
						value = setDefaultValue(new PropertyTokenHolder(tokens.actualName));
					}
					else {
						throw new NullValueInNestedPathException(getRootClass(), this.nestedPath + propertyName,
								"Cannot access indexed value of property referenced in indexed " +
										"property path '" + propertyName + "': returned null");
					}
				}
				StringBuilder indexedPropertyName = new StringBuilder(tokens.actualName);
				// apply indexes and map keys
				for (int i = 0; i < tokens.keys.length; i++) {
					String key = tokens.keys[i];
					if (value == null) {
						throw new NullValueInNestedPathException(getRootClass(), this.nestedPath + propertyName,
								"Cannot access indexed value of property referenced in indexed " +
										"property path '" + propertyName + "': returned null");
					}
					else if (value.getClass().isArray()) {
						int index = Integer.parseInt(key);
						value = growArrayIfNecessary(value, index, indexedPropertyName.toString());
						value = Array.get(value, index);
					}

View on GitHub (pinned to e8729d0438)

Solutions

  1. Initialize the collection/map/array field (field initializer or constructor).
  2. Enable wrapper.setAutoGrowNestedPaths(true) if you want tolerant auto-creation on read.
  3. Guard the read with a null check on the enclosing property first.

Example fix

// before
public class Bag { private Map<String,String> props; ... } // null
Object v = wrapper.getPropertyValue("props['k']");

// after
public class Bag { private Map<String,String> props = new HashMap<>(); ... }
Defensive patterns

Strategy: validation

Validate before calling

String base = propertyName.replaceAll("\\[.*$", "");
Object holder = wrapper.getPropertyValue(base);
if (holder != null || wrapper.isAutoGrowNestedPaths()) {
    wrapper.getPropertyValue(propertyName);
}

Type guard

static boolean holderNotNull(BeanWrapper bw, String path) {
    return bw.getPropertyValue(path.replaceAll("\\[.*$", "")) != null;
}

Try / catch

try { Object v = wrapper.getPropertyValue(propertyName); }
catch (NullValueInNestedPathException ex) { v = null; }

Prevention

When it happens

Trigger: getProperty("items[0]") where getItems() returns null; reading 'config["key"]' when getConfig() is null; a SpEL/BeanWrapper read against a freshly created bean with uninitialized collection fields.

Common situations: Reading nested indexed values before the collection has been populated; DTOs with nullable collection fields; disabling autoGrowNestedPaths while still expecting tolerant reads.

Related errors


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