spring-projects/spring-ai · error · IllegalArgumentException

Bean must not be null

Error message

Bean must not be null

What it means

The callback builder requires the bean instance on which the consumer method will be invoked. validate() throws this when the bean field is null. Without a bean the reflective Method.invoke target is undefined, so building is refused.

Source

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

		 * @param resourceListChanged The resource list changed annotation
		 * @return This builder
		 */
		@SuppressWarnings("unchecked")
		public T resourceListChanged(McpResourceListChanged resourceListChanged) {
			// 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

  1. Call .bean(...) with the object instance that declares the consumer method.
  2. Ensure dependency injection succeeded so the bean reference is not null.
  3. Use the annotation-driven registration path, which wires method and bean together automatically.

Example fix

// before
new AsyncMcpResourceListChangedMethodCallback.Builder().method(m).build();
// after
new AsyncMcpResourceListChangedMethodCallback.Builder().method(m).bean(handler).build();
Defensive patterns

Strategy: validation

Validate before calling

Objects.requireNonNull(bean, "bean must be set before build()");

Try / catch

try {
    callback = builder.build();
} catch (IllegalArgumentException e) {
    if (e.getMessage().contains("Bean must not be null")) {
        throw new IllegalStateException("Builder misconfigured: call .bean(...) before .build()", e);
    }
    throw e;
}

Prevention

When it happens

Trigger: Building a Sync/AsyncMcpResourceListChangedMethodCallback with .method(...) set but omitting .bean(Object).

Common situations: Programmatic registration where the developer passes a null Spring bean (failed injection) or forgets the .bean(...) call when constructing the builder by hand.

Related errors


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