spring-projects/spring-ai · error · IllegalArgumentException

Method must not be null

Error message

Method must not be null

What it means

AbstractMcpElicitationMethodCallback.validateMethod throws this IllegalArgumentException when the Method passed to an elicitation callback constructor is null. The callback needs a concrete reflected method to validate and later invoke when the client responds to an elicitation.

Source

Thrown at mcp/mcp-annotations/src/main/java/org/springframework/ai/mcp/annotation/method/elicitation/AbstractMcpElicitationMethodCallback.java:66

		Assert.notNull(method, "Method can't be null!");
		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 elicitation 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 elicitation 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 and
	 * delegates exchange type checking to subclasses.

View on GitHub (pinned to 98a7beda4f)

Solutions

  1. Pass a valid non-null Method instance from your scanner to the callback constructor
  2. Fix the method lookup: verify the exact method name and parameter types in getDeclaredMethod
  3. Log/handle NoSuchMethodException at the lookup site instead of propagating null

Example fix

// before
Method m = null; // lookup failed
new SyncMcpElicitationMethodCallback(bean, m, "id");
// after
Method m = bean.getClass().getDeclaredMethod("onElicit", ElicitRequest.class);
new SyncMcpElicitationMethodCallback(bean, m, "id");
Defensive patterns

Strategy: validation

Validate before calling

Method m = lookupElicitationMethod(bean, elicitId);
if (m == null) throw new IllegalStateException("no @McpElicitation method found for id " + elicitId);

Type guard

static Optional<Method> findElicitMethod(Object bean) {
    return Arrays.stream(bean.getClass().getDeclaredMethods())
        .filter(m -> m.isAnnotationPresent(McpElicitation.class))
        .findFirst();
}

Try / catch

try { new SyncMcpElicitationMethodCallback(bean, method, elicitId); }
catch (IllegalArgumentException e) { log.error("elicitation callback construction failed: {}", e.getMessage()); throw e; }

Prevention

When it happens

Trigger: Building the callback with a null Method, e.g. a lookup by annotation/name that fails silently and returns null before calling new SyncMcpElicitationMethodCallback(bean, null, ...).

Common situations: Custom annotation scanners that use getDeclaredMethod with a misspelled name or wrong parameter types (throwing NoSuchMethodException swallowed elsewhere), leading to a null Method handoff.

Related errors


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