{"record":{"id":"a2f56e93d67a3ba4","repo":"spring-projects/spring-ai","slug":"bean-must-not-be-null-a2f56e","errorCode":null,"errorMessage":"Bean must not be null","messagePattern":"Bean must not be null","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"mcp/mcp-annotations/src/main/java/org/springframework/ai/mcp/annotation/method/logging/AbstractMcpLoggingMethodCallback.java","lineNumber":241,"sourceCode":"\t\t * @param loggingConsumer The logging consumer annotation\n\t\t * @return This builder\n\t\t */\n\t\t@SuppressWarnings(\"unchecked\")\n\t\tpublic T loggingConsumer(McpLogging loggingConsumer) {\n\t\t\t// No additional configuration needed from the annotation at this time\n\t\t\treturn (T) this;\n\t\t}\n\n\t\t/**\n\t\t * Validate the builder state.\n\t\t * @throws IllegalArgumentException if the builder state is invalid\n\t\t */\n\t\tprotected void validate() {\n\t\t\tif (this.method == null) {\n\t\t\t\tthrow new IllegalArgumentException(\"Method must not be null\");\n\t\t\t}\n\t\t\tif (this.bean == null) {\n\t\t\t\tthrow new IllegalArgumentException(\"Bean must not be null\");\n\t\t\t}\n\t\t}\n\n\t\t/**\n\t\t * Build the callback.\n\t\t * @return A new callback instance\n\t\t */\n\t\tpublic abstract R build();\n\n\t}\n\n}\n","sourceCodeStart":223,"sourceCodeEnd":254,"githubUrl":"https://github.com/spring-projects/spring-ai/blob/98a7beda4f29d80a71c5837eb4053b03a93a46f7/mcp/mcp-annotations/src/main/java/org/springframework/ai/mcp/annotation/method/logging/AbstractMcpLoggingMethodCallback.java#L223-L254","documentation":"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.","triggerScenarios":"Calling build() on a logging callback Builder after setting method(...) but without calling bean(Object).","commonSituations":"Manual registration outside a DI container where no bean instance is supplied; bean factory returned null; copy-pasted builder setup omitted the bean call.","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"],"exampleFix":"// before\nCallback cb = new Builder().method(m).build(); // throws\n// after\nCallback cb = new Builder().method(m).bean(logHandler).build();","handlingStrategy":"validation","validationCode":"Object handler = applicationContext.getBean(LogHandler.class);\nif (handler == null) { throw new IllegalStateException(\"bean must be set before build()\"); }\nnew Builder().method(m).bean(handler).build();","typeGuard":"static boolean hasBean(Object bean) { return bean != null; }","tryCatchPattern":null,"preventionTips":["Set bean(...) before build(); the callback invokes methods on this instance","Verify the handler bean exists in the DI container before registering callbacks"],"tags":["mcp","logging","builder","null-check","java"],"backgroundTag":"null-argument","analyzedSha":"98a7beda4f29d80a71c5837eb4053b03a93a46f7","analyzedAt":"2026-09-11T14:15:49.441Z","contentChangedAt":"2026-09-11T14:15:49.441Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}