spring-projects/spring-ai · error · IllegalArgumentException

Method must not be null

Error message

Method must not be null

What it means

AbstractMcpPromptListChangedMethodCallback.validateMethod rejects a null Method reference before validating the prompt-list-changed handler signature. Registration must pass a concrete reflective Method for the callback to invoke; null indicates a programming error in registration.

Source

Thrown at mcp/mcp-annotations/src/main/java/org/springframework/ai/mcp/annotation/method/changed/prompt/AbstractMcpPromptListChangedMethodCallback.java:68

		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 prompt 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 prompt 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. Ensure the handler method exists and the reflective lookup returns a non-null Method before constructing the callback
  2. Add an assertion/log at the lookup site so null Methods are caught before registration
  3. Pass a compile-time-safe method reference or a Class+methodName pair verified via getMethod

Example fix

// before
Method m = customLookup(clazz, "onPromptsChanged"); // may return null
new DefaultMcpPromptListChangedMethodCallback(m);
// after
Method m = Objects.requireNonNull(customLookup(clazz, "onPromptsChanged"), "handler method not found");
new DefaultMcpPromptListChangedMethodCallback(m);
Defensive patterns

Strategy: validation

Validate before calling

Objects.requireNonNull(method, "Prompt list changed handler method must not be null");

Type guard

if (method == null) { throw new IllegalArgumentException("handler method missing"); }

Try / catch

try { registerListChangedCallback(handler); } catch (IllegalArgumentException e) { log.error("Bad handler registration: " + e.getMessage()); }

Prevention

When it happens

Trigger: Passing null to the constructor/registration API of an McpPromptListChanged callback (e.g. when a reflective method lookup fails silently and null is forwarded).

Common situations: Dynamic bean wiring where a Method lookup by name returns null; refactoring renamed the handler method while registration still looked up the old name.

Related errors


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