{"record":{"id":"3e4b724fdc3dd4a0","repo":"alibaba/spring-ai-alibaba","slug":"chatclient-must-not-be-null","errorCode":null,"errorMessage":"ChatClient must not be null","messagePattern":"ChatClient must not be null","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"spring-ai-alibaba-agent-framework/src/main/java/com/alibaba/cloud/ai/graph/agent/tools/WebFetchTool.java","lineNumber":426,"sourceCode":"\t}\n\n\tpublic static class Builder {\n\n\t\tprivate final ChatClient chatClient;\n\n\t\tprivate int maxContentLength = 100_000;\n\n\t\tprivate int maxCacheSize = 100;\n\n\t\tprivate int maxRetries = 2;\n\n\t\tprivate String name = \"web_fetch\";\n\n\t\tprivate String description = DEFAULT_TOOL_DESCRIPTION;\n\n\t\tprivate Builder(ChatClient chatClient) {\n\t\t\tif (chatClient == null) {\n\t\t\t\tthrow new IllegalArgumentException(\"ChatClient must not be null\");\n\t\t\t}\n\t\t\tthis.chatClient = chatClient;\n\t\t}\n\n\t\tpublic Builder maxContentLength(int maxContentLength) {\n\t\t\tif (maxContentLength <= 0) {\n\t\t\t\tthrow new IllegalArgumentException(\"maxContentLength must be positive\");\n\t\t\t}\n\t\t\tthis.maxContentLength = maxContentLength;\n\t\t\treturn this;\n\t\t}\n\n\t\tpublic Builder maxCacheSize(int maxCacheSize) {\n\t\t\tif (maxCacheSize <= 0) {\n\t\t\t\tthrow new IllegalArgumentException(\"maxCacheSize must be positive\");\n\t\t\t}\n\t\t\tthis.maxCacheSize = maxCacheSize;\n\t\t\treturn this;","sourceCodeStart":408,"sourceCodeEnd":444,"githubUrl":"https://github.com/alibaba/spring-ai-alibaba/blob/f82da0b50f35744c13968191be2b1cd2452ef550/spring-ai-alibaba-agent-framework/src/main/java/com/alibaba/cloud/ai/graph/agent/tools/WebFetchTool.java#L408-L444","documentation":"WebFetchTool.Builder's package-private constructor requires a non-null ChatClient (the tool uses it to summarize/process fetched content). Passing null throws IllegalArgumentException('ChatClient must not be null') at construction time — a fail-fast programming-error guard.","triggerScenarios":"Calling WebFetchTool.builder(null), or passing a ChatClient field/bean that has not been initialized (still null) at wiring time.","commonSituations":"Forgetting to @Autowired or inject the ChatClient bean; constructing the tool in a static initializer before the Spring context is ready; refactoring that removed the client assignment.","solutions":["Pass a properly built ChatClient, e.g. ChatClient.builder(chatModel).build().","Ensure the ChatClient bean is injected before the tool is constructed (constructor injection).","Add a null check or @AssertNonNull in your configuration class before builder() call.","If the bean may be absent, make the tool creation conditional (@ConditionalOnBean)."],"exampleFix":"// before\nWebFetchTool.builder(null).build();\n// after\nChatClient chatClient = ChatClient.builder(chatModel).build();\nWebFetchTool tool = WebFetchTool.builder(chatClient).build();","handlingStrategy":"type-guard","validationCode":"if (chatClient == null) {\n    throw new IllegalStateException(\"ChatClient bean must be initialized before building WebFetchTool\");\n}\nWebFetchTool tool = WebFetchTool.builder(chatClient).build();","typeGuard":"boolean hasChatClient(ChatClient c) { return c != null; }","tryCatchPattern":"try {\n    tool = WebFetchTool.builder(chatClient).build();\n} catch (IllegalArgumentException e) {\n    if (\"ChatClient must not be null\".equals(e.getMessage())) {\n        throw new BeanInitializationException(\"ChatClient missing; wire chatModel first\", e);\n    }\n    throw e;\n}","preventionTips":["Inject ChatClient via constructor so it can never be null at build time","Build the tool inside a Spring @Configuration/@Bean method after context wiring","Add startup assertions for required beans"],"tags":["null-check","builder","configuration"],"backgroundTag":"null-argument","analyzedSha":"f82da0b50f35744c13968191be2b1cd2452ef550","analyzedAt":"2026-09-09T15:32:42.421Z","contentChangedAt":"2026-09-09T15:32:42.421Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}