{"record":{"id":"1349d4b11015b8d7","repo":"spring-projects/spring-ai","slug":"method-must-not-be-null-1349d4","errorCode":null,"errorMessage":"Method must not be null","messagePattern":"Method 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":144,"sourceCode":"\t\tthis.uriTemplateManager = uriTemplateMangerFactory.create(this.uri);\n\n\t\tthis.uriVariables = this.uriTemplateManager.getVariableNames();\n\n\t\tthis.contentType = contentType;\n\t\tthis.meta = meta;\n\t}\n\n\t/**\n\t * Validates that the method signature is compatible with the resource callback.\n\t * <p>\n\t * This method checks that the return type is valid and that the parameters match the\n\t * expected pattern based on whether URI variables are present.\n\t * @param method The method to validate\n\t * @throws IllegalArgumentException if the method signature is not compatible\n\t */\n\tprotected void validateMethod(Method method) {\n\t\tif (method == null) {\n\t\t\tthrow new IllegalArgumentException(\"Method must not be null\");\n\t\t}\n\n\t\tthis.validateReturnType(method);\n\n\t\tif (this.uriVariables.isEmpty()) {\n\t\t\tthis.validateParametersWithoutUriVariables(method);\n\t\t}\n\t\telse {\n\t\t\tthis.validateParametersWithUriVariables(method);\n\t\t}\n\t}\n\n\t/**\n\t * Validates that the method return type is compatible with the resource callback.\n\t * This method should be implemented by subclasses to handle specific return type\n\t * validation.\n\t * @param method The method to validate\n\t * @throws IllegalArgumentException if the return type is not compatible","sourceCodeStart":126,"sourceCodeEnd":162,"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#L126-L162","documentation":"AbstractMcpResourceMethodCallback.validateMethod was invoked with a null Method reference. Every registered resource method must be a resolved java.lang.reflect.Method for the framework to validate its signature and build invocation arguments, so a null is rejected with IllegalArgumentException.","triggerScenarios":"Programmatically constructing an McpResourceMethodCallback (builder or constructor) without calling .method(...)/passing null; reflection lookups that fail silently and return null before registration.","commonSituations":"Manual wiring of resource method callbacks instead of using annotation scanning; tests assembling callbacks by hand; refactors that removed the method setter call.","solutions":["Pass the actual annotated Method object when building the callback (e.g. getClass().getDeclaredMethod(...)).","Check the builder chain for a missing .method(...) call before execute/build.","Fix any reflection code returning null and add a null check before registration."],"exampleFix":"// before\nnew McpResourceMethodCallback.Builder().uriVariables(uris).build(); // method missing\n\n// after\nMethod m = MyResources.class.getDeclaredMethod(\"readFile\", String.class);\nnew McpResourceMethodCallback.Builder().method(m).uriVariables(uris).build();","handlingStrategy":"validation","validationCode":"Objects.requireNonNull(method, \"Method must be resolved before building McpResourceMethodCallback\");","typeGuard":null,"tryCatchPattern":"Method m = resolveMethod(resourceClass);\nif (m == null) {\n    throw new IllegalStateException(\"@McpResource method not found\");\n}\ncallback = new McpResourceMethodCallback.Builder().method(m).build();","preventionTips":["Always set .method(...) on the callback builder.","Check reflection results for null before registering.","Prefer annotation scanning over manual callback construction."],"tags":["mcp","java","null-check","resource-method","registration"],"backgroundTag":"null-argument","analyzedSha":"98a7beda4f29d80a71c5837eb4053b03a93a46f7","analyzedAt":"2026-09-11T14:15:49.441Z","contentChangedAt":"2026-09-11T14:15:49.441Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}