spring-projects/spring-ai · error · java.lang.IllegalArgumentException
Method must not be null
Error message
Method must not be null
What it means
AbstractMcpPromptMethodCallback.validateMethod() checks that the Method resolved for an @McpPrompt-annotated callback is non-null before validating its return type and parameters. A null Method means the callback was built without a target method, which cannot be invoked reflectively, so construction fails fast with IllegalArgumentException.
Source
Thrown at mcp/mcp-annotations/src/main/java/org/springframework/ai/mcp/annotation/method/prompt/AbstractMcpPromptMethodCallback.java:81
* @param method The method to create a callback for
* @param bean The bean instance that contains the method
* @param prompt The prompt
*/
protected AbstractMcpPromptMethodCallback(Method method, Object bean, Prompt prompt) {
this.method = method;
this.bean = bean;
this.prompt = prompt;
this.validateMethod(this.method);
}
/**
* Validates that the method signature is compatible with the prompt callback.
* @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 callback.
* @param method The method to validate
* @throws IllegalArgumentException if the return type is not compatible
*/
protected abstract void validateReturnType(Method method);
/**
* Checks if a parameter type is compatible with the exchange type.
* @param paramType The parameter type to check
* @return true if the parameter type is compatible with the exchange type, false
* otherwiseView on GitHub (pinned to 98a7beda4f)
Solutions
- Ensure the Builder's method(...) is called with a non-null java.lang.reflect.Method before building the callback.
- Fix the method-lookup code to throw when the annotated method cannot be found rather than returning null.
- Validate the resolved Method for null before passing it into the callback constructor.
Example fix
// before
Method m = clazz.getDeclaredMethod("promptHandler"); // throws if absent, or lookup returns null
callback = MyPromptCallback.builder().method(maybeNull).bean(bean).build();
// after
Objects.requireNonNull(m, "Resolved @McpPrompt method must not be null");
callback = MyPromptCallback.builder().method(m).bean(bean).build(); Defensive patterns
Strategy: validation
Validate before calling
Method m = lookupPromptMethod(bean); Objects.requireNonNull(m, "@McpPrompt method must not be null");
Type guard
Optional<Method> findMethod(Class<?> c, String name) { return Arrays.stream(c.getDeclaredMethods()).filter(m -> m.getName().equals(name)).findFirst(); } Try / catch
try { callback = builder().method(m).bean(bean).build(); } catch (IllegalArgumentException e) { log.error("Callback construction failed: {}", e.getMessage()); } Prevention
- Always call .method(...) on the builder before build()
- Make method lookups throw NoSuchMethodException instead of returning null
- Resolve Methods once at startup and fail fast on missing annotated methods
When it happens
Trigger: Calling AbstractMcpPromptMethodCallback's constructor (directly or via a Builder.build()) with method = null, e.g. a builder whose method field was never set or was resolved to null by a method-lookup helper.
Common situations: Reflection-based lookup that fails silently and returns null instead of throwing; custom builder usage forgetting the .method(...) call; annotation scanning code passing through a null from getDeclaredMethod on a missing method name.
Related errors
- Method must not be null
- Method must not be null
- Method must not be null
- Error invoking resource list changed consumer method: " + th
- Error invoking tool list changed consumer method:
AI-assisted analysis of spring-projects/spring-ai@98a7beda4f (2026-09-11).
Data as JSON: /api/errors/2463f91f635a4383.
Report an issue: GitHub.