{"record":{"id":"4121a1d083fafcb5","repo":"spring-projects/spring-ai","slug":"method-must-not-be-null-4121a1","errorCode":null,"errorMessage":"Method must not be null","messagePattern":"Method 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/sampling/AbstractMcpSamplingMethodCallback.java","lineNumber":66,"sourceCode":"\t\tAssert.notNull(method, \"Method can't be null!\");\n\t\tAssert.notNull(bean, \"Bean can't be null!\");\n\n\t\tthis.method = method;\n\t\tthis.bean = bean;\n\t\tthis.validateMethod(this.method);\n\t}\n\n\t/**\n\t * Validates that the method signature is compatible with the sampling 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.\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\t\tthis.validateParameters(method);\n\t}\n\n\t/**\n\t * Validates that the method return type is compatible with the sampling 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\n\t */\n\tprotected abstract void validateReturnType(Method method);\n\n\t/**\n\t * Validates method parameters. This method provides common validation logic and\n\t * delegates exchange type checking to subclasses.","sourceCodeStart":48,"sourceCodeEnd":84,"githubUrl":"https://github.com/spring-projects/spring-ai/blob/98a7beda4f29d80a71c5837eb4053b03a93a46f7/mcp/mcp-annotations/src/main/java/org/springframework/ai/mcp/annotation/method/sampling/AbstractMcpSamplingMethodCallback.java#L48-L84","documentation":"validateMethod in AbstractMcpSamplingMethodCallback rejects a null Method reference before validating its return type and parameters. Sampling/elicitation callbacks must be built around a concrete annotated method.","triggerScenarios":"Passing null to the sampling/elicitation callback constructor or to validateMethod, typically when reflection fails to resolve the annotated method or a builder is given no method.","commonSituations":"Custom registration code looking up a Method by name that does not exist; framework wiring bug where the annotated bean/method is absent; tests constructing the callback directly with null.","solutions":["Pass the resolved java.lang.reflect.Method of your @McpSampling/@McpElicitation annotated method","Fix the reflection lookup (correct method name and parameter types) so it does not return null","Ensure the annotated class is registered on the classpath and scanned by the framework"],"exampleFix":"// before\nnew AbstractMcpElicitationMethodCallback(null, bean, ...);\n// after\nMethod m = MyHandlers.class.getDeclaredMethod(\"myElicitation\", ...);\nnew AbstractMcpElicitationMethodCallback(m, bean, ...);","handlingStrategy":"type-guard","validationCode":"if (method == null) {\n    throw new IllegalArgumentException(\"Annotated sampling/elicitation method must be resolved before building callback\");\n}","typeGuard":"boolean isResolved(Method m) {\n    return m != null && m.getDeclaringClass() != null;\n}","tryCatchPattern":"try {\n    Method m = type.getDeclaredMethod(name, paramTypes);\n    return buildCallback(m, bean);\n} catch (NoSuchMethodException e) {\n    throw new IllegalStateException(\"Sampling method not found: \" + name, e);\n}","preventionTips":["Never pass a possibly-null Method into callback constructors","Check reflection lookups for NoSuchMethodException instead of swallowing them","Verify annotated handler classes are present on the classpath at startup"],"tags":["mcp","java","null-check","reflection"],"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"}