spring-projects/spring-ai · error · IllegalArgumentException
Bean must not be null
Error message
Bean must not be null
What it means
The builder's validate() method (in AbstractMcpLoggingMethodCallback's Builder) throws IllegalArgumentException when bean is null at build() time. The callback needs the bean instance on which to invoke the annotated logging method.
Source
Thrown at mcp/mcp-annotations/src/main/java/org/springframework/ai/mcp/annotation/method/logging/AbstractMcpLoggingMethodCallback.java:241
* @param loggingConsumer The logging consumer annotation
* @return This builder
*/
@SuppressWarnings("unchecked")
public T loggingConsumer(McpLogging loggingConsumer) {
// No additional configuration needed from the annotation at this time
return (T) this;
}
/**
* Validate the builder state.
* @throws IllegalArgumentException if the builder state is invalid
*/
protected void validate() {
if (this.method == null) {
throw new IllegalArgumentException("Method must not be null");
}
if (this.bean == null) {
throw new IllegalArgumentException("Bean must not be null");
}
}
/**
* Build the callback.
* @return A new callback instance
*/
public abstract R build();
}
}
View on GitHub (pinned to 98a7beda4f)
Solutions
- Call bean(...) with the instance that declares the @McpLogging method before build()
- Ensure the bean is instantiated (e.g. retrieved from the application context) and non-null
- If using DI, verify component scanning actually creates the handler bean
Example fix
// before Callback cb = new Builder().method(m).build(); // throws // after Callback cb = new Builder().method(m).bean(logHandler).build();
Defensive patterns
Strategy: validation
Validate before calling
Object handler = applicationContext.getBean(LogHandler.class);
if (handler == null) { throw new IllegalStateException("bean must be set before build()"); }
new Builder().method(m).bean(handler).build(); Type guard
static boolean hasBean(Object bean) { return bean != null; } Prevention
- Set bean(...) before build(); the callback invokes methods on this instance
- Verify the handler bean exists in the DI container before registering callbacks
When it happens
Trigger: Calling build() on a logging callback Builder after setting method(...) but without calling bean(Object).
Common situations: Manual registration outside a DI container where no bean instance is supplied; bean factory returned null; copy-pasted builder setup omitted the bean call.
Related errors
- Method must not be null
- Method must not be null
- Request must not be null
- Request must not be null
- Method must not be null
AI-assisted analysis of spring-projects/spring-ai@98a7beda4f (2026-09-11).
Data as JSON: /api/errors/a2f56e93d67a3ba4.
Report an issue: GitHub.