{"record":{"id":"29f790fc185df434","repo":"spring-projects/spring-ai","slug":"bean-must-not-be-null-29f790","errorCode":null,"errorMessage":"Bean must not be null","messagePattern":"Bean must not be null","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"mcp/mcp-annotations/src/main/java/org/springframework/ai/mcp/annotation/method/resource/AbstractMcpResourceMethodCallback.java","lineNumber":723,"sourceCode":"\t\t * Set the MIME type of the resource.\n\t\t * @param mimeType The MIME type of the resource\n\t\t * @return This builder\n\t\t */\n\t\tpublic T mimeType(String mimeType) {\n\t\t\tthis.mimeType = mimeType;\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\tif (this.uri == null || this.uri.isEmpty()) {\n\t\t\t\tthrow new IllegalArgumentException(\"URI must not be null or empty\");\n\t\t\t}\n\t\t\tif (this.uriTemplateManagerFactory == null) {\n\t\t\t\tthis.uriTemplateManagerFactory = new DefaultMcpUriTemplateManagerFactory();\n\t\t\t}\n\t\t\tif (this.mimeType == null) {\n\t\t\t\tthis.mimeType = \"text/plain\";\n\t\t\t}\n\n\t\t\tif (this.name == null) {\n\t\t\t\tthis.name = this.method.getName();\n\t\t\t}\n\t\t}\n\n\t\t/**\n\t\t * Build the callback.","sourceCodeStart":705,"sourceCodeEnd":741,"githubUrl":"https://github.com/spring-projects/spring-ai/blob/98a7beda4f29d80a71c5837eb4053b03a93a46f7/mcp/mcp-annotations/src/main/java/org/springframework/ai/mcp/annotation/method/resource/AbstractMcpResourceMethodCallback.java#L705-L741","documentation":"Builder-state validation error thrown when the target bean (the object holding the @McpResource method) is null when building an AbstractMcpResourceMethodCallback. The framework needs the bean instance to invoke the method on.","triggerScenarios":"Calling builder().method(m).uri(u).build() without .bean(...) — typically in programmatic registration where the bean instance wasn't resolved or a dependency was null.","commonSituations":"Manual registration in tests with a null bean; Spring bean not yet injected (field null at registration time); component scanned incorrectly so the handler object is missing.","solutions":["Pass the handler instance: .bean(handlerObject) before build()","Verify the bean is injected/constructed before MCP registration runs (check @Autowired/@Component wiring)","Assert non-null in your registration code and fail early with a clear message"],"exampleFix":"// before\nMcpSyncResourceMethodCallback.builder().method(m).uri(\"docs/{id}\").build();\n// after\nMcpSyncResourceMethodCallback.builder().bean(myResourceHandler).method(m).uri(\"docs/{id}\").build();","handlingStrategy":"validation","validationCode":"Objects.requireNonNull(bean, \"bean must be set on the resource callback builder\");","typeGuard":"if (handler == null) { throw new IllegalStateException(\"Handler bean not injected before MCP registration\"); }","tryCatchPattern":"try {\n    var cb = builder().bean(bean).method(method).uri(uri).build();\n} catch (IllegalArgumentException e) {\n    if (e.getMessage().equals(\"Bean must not be null\")) throw new IllegalStateException(\"Builder misconfiguration: set .bean(...)\", e);\n    throw e;\n}","preventionTips":["Ensure the handler bean is fully constructed before registration runs","Use constructor injection so the bean cannot be null at registration time","Run an application-startup smoke test that registers all resources"],"tags":["mcp","java","builder","validation","null"],"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"}