{"record":{"id":"5820d1efbbf9091f","repo":"alibaba/spring-ai-alibaba","slug":"client-cannot-be-null","errorCode":null,"errorMessage":"client cannot be null","messagePattern":"client cannot be null","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"spring-ai-alibaba-graph-core/src/main/java/com/alibaba/cloud/ai/graph/checkpoint/savers/mongo/MongoSaver.java","lineNumber":544,"sourceCode":"\n\t\tpublic Builder client(MongoClient client) {\n\t\t\tthis.client = client;\n\t\t\treturn this;\n\t\t}\n\n\t\tpublic Builder stateSerializer(StateSerializer stateSerializer) {\n\t\t\tthis.stateSerializer = stateSerializer;\n\t\t\treturn this;\n\t\t}\n\n\t\t/**\n\t\t * Builds a new MongoSaver instance.\n\t\t * @return a new MongoSaver instance\n\t\t * @throws IllegalArgumentException if client or stateSerializer is null\n\t\t */\n\t\tpublic MongoSaver build() {\n\t\t\tif (client == null) {\n\t\t\t\tthrow new IllegalArgumentException(\"client cannot be null\");\n\t\t\t}\n\t\t\tif (stateSerializer == null) {\n\t\t\t\tthis.stateSerializer = StateGraph.DEFAULT_JACKSON_SERIALIZER;\n\t\t\t}\n\t\t\treturn new MongoSaver(client, stateSerializer);\n\t\t}\n\t}\n\n}\n","sourceCodeStart":526,"sourceCodeEnd":554,"githubUrl":"https://github.com/alibaba/spring-ai-alibaba/blob/f82da0b50f35744c13968191be2b1cd2452ef550/spring-ai-alibaba-graph-core/src/main/java/com/alibaba/cloud/ai/graph/checkpoint/savers/mongo/MongoSaver.java#L526-L554","documentation":"MongoSaver.Builder.build() validates that a MongoClient was supplied; if client is null it throws IllegalArgumentException('client cannot be null'). MongoSaver cannot function without a client connection, so this is a fail-fast constructor guard. If stateSerializer is null, a default Jackson serializer is used instead of throwing.","triggerScenarios":"Calling MongoSaver.builder().build() without calling .client(...); a MongoClient field/bean that is null at build time (failed Spring bean wiring, @Autowired-by-constructor not yet injected, or configuration that skipped client creation).","commonSituations":"Misconfigured Spring context where the MongoClient bean wasn't created (missing spring-boot-starter-data-mongodb or connection properties); constructing the saver manually in a main() and forgetting the client; conditional bean creation that silently produced null.","solutions":["Pass a client: MongoSaver.builder().client(mongoClient).build()","Verify the MongoClient bean exists and is injected before building the saver (check MongoClients.create(...) ran successfully)","If using Spring, confirm the MongoDB starter/connection config so the auto-configured client is available"],"exampleFix":"// before\nMongoSaver saver = MongoSaver.builder().build(); // throws\n// after\nMongoClient client = MongoClients.create(\"mongodb://localhost:27017\");\nMongoSaver saver = MongoSaver.builder().client(client).build();","handlingStrategy":"validation","validationCode":"Objects.requireNonNull(mongoClient, \"MongoClient must be created before MongoSaver.builder().build()\");\nMongoSaver saver = MongoSaver.builder().client(mongoClient).build();","typeGuard":"boolean canBuildSaver(MongoClient client) {\n    return client != null;\n}","tryCatchPattern":"try {\n    saver = MongoSaver.builder().client(client).build();\n} catch (IllegalArgumentException e) {\n    if (e.getMessage().contains(\"client cannot be null\")) {\n        throw new IllegalStateException(\"MongoClient bean missing — check Mongo auto-configuration/connection properties\", e);\n    }\n    throw e;\n}","preventionTips":["Always chain .client(...) before .build()","Verify MongoClient bean injection (non-null) before building the saver","Fail application startup early if the Mongo connection config is absent"],"tags":["mongo","builder","null-client","misconfiguration"],"backgroundTag":"invalid-constructor-argument","analyzedSha":"f82da0b50f35744c13968191be2b1cd2452ef550","analyzedAt":"2026-09-09T15:32:42.421Z","contentChangedAt":"2026-09-09T15:32:42.421Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}