spring-projects/spring-ai · error · IllegalArgumentException

Method must not be null

Error message

Method must not be null

What it means

IllegalArgumentException thrown by AbstractMcpResourceListChangedMethodCallback.validateMethod() when a null Method is passed during callback construction. validateMethod runs from the constructor (null check first, then return-type and parameter validation), so any builder/registration path that fails to resolve the handler's reflective Method hits this before any signature checks.

Source

Thrown at mcp/mcp-annotations/src/main/java/org/springframework/ai/mcp/annotation/method/changed/resource/AbstractMcpResourceListChangedMethodCallback.java:69

		Assert.notNull(bean, "Bean can't be null!");

		this.method = method;
		this.bean = bean;
		this.validateMethod(this.method);
	}

	/**
	 * Validates that the method signature is compatible with the resource list changed
	 * consumer callback.
	 * <p>
	 * This method checks that the return type is valid and that the parameters match the
	 * expected pattern.
	 * @param method The method to validate
	 * @throws IllegalArgumentException if the method signature is not compatible
	 */
	protected void validateMethod(Method method) {
		if (method == null) {
			throw new IllegalArgumentException("Method must not be null");
		}

		this.validateReturnType(method);
		this.validateParameters(method);
	}

	/**
	 * Validates that the method return type is compatible with the resource list changed
	 * consumer callback. This method should be implemented by subclasses to handle
	 * specific return type validation.
	 * @param method The method to validate
	 * @throws IllegalArgumentException if the return type is not compatible
	 */
	protected abstract void validateReturnType(Method method);

	/**
	 * Validates method parameters. This method provides common validation logic.
	 * @param method The method to validate

View on GitHub (pinned to 98a7beda4f)

Solutions

  1. Pass the resolved java.lang.reflect.Method of the @McpResourceListChanged handler to the builder/constructor.
  2. Check that the reflective lookup used the correct method name and parameter types and asserted non-null before constructing the callback.
  3. Rely on the annotation-driven registration path so the framework resolves the method automatically.
  4. Add an Objects.requireNonNull(method) assertion at the lookup site to fail with a clearer message.

Example fix

// before
Method m = clazz.getDeclaredMethod("onResourcesChanged"); // wrong params -> null passed on / lookup throws
callbackBuilder.method(m);
// after
Method m = clazz.getDeclaredMethod("onResourcesChanged", List.class);
Objects.requireNonNull(m, "resource list changed handler not found");
callbackBuilder.method(m);
Defensive patterns

Strategy: validation

Validate before calling

Method m = clazz.getDeclaredMethod("onResourcesChanged", List.class);
Objects.requireNonNull(m, "resource list changed handler method not found on " + clazz.getName());
new MyResourceListChangedCallback(m, bean);

Try / catch

try {
    callback = new SyncMcpResourceListChangedMethodCallback(method, bean);
} catch (IllegalArgumentException e) {
    if (e.getMessage().contains("Method must not be null")) {
        throw new ConfigurationException("Resource list changed method resolution failed — check name/signature", e);
    }
    throw e;
}

Prevention

When it happens

Trigger: Constructing a resource-list-changed callback (sync or async subclass) with method(null), or a reflective lookup (getDeclaredMethod/getMethod) returning null being passed through unchecked. The constructor is the caller, so this surfaces at object creation time.

Common situations: Method-name/signature lookups failing after a refactor or SDK upgrade; manual builder wiring omitting the method; conditional method resolution returning null in test harnesses; annotation metadata missing so the scanner yields null.

Related errors


AI-assisted analysis of spring-projects/spring-ai@98a7beda4f (2026-09-11). Data as JSON: /api/errors/480c81ee1cbd7474. Report an issue: GitHub.