{"record":{"id":"038f7719bfe14f0d","repo":"alibaba/spring-ai-alibaba","slug":"strategy-factory-cannot-be-null","errorCode":null,"errorMessage":"Strategy factory cannot be null","messagePattern":"Strategy factory cannot 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/flow/strategy/FlowGraphBuildingStrategyRegistry.java","lineNumber":87,"sourceCode":"\t\t\tthrow new IllegalArgumentException(\"Strategy type '\" + type + \"' is already registered\");\n\t\t}\n\n\t\tstrategyFactories.put(type, () -> strategy);\n\t}\n\n\t/**\n\t * Registers a strategy factory. Each call to {@link #createStrategy(String)} or\n\t * {@link #getStrategy(String)} will use the factory to obtain a strategy instance.\n\t * @param type the strategy type\n\t * @param factory the factory that creates strategy instances\n\t * @throws IllegalArgumentException if type or factory is null, or type is already registered\n\t */\n\tpublic void registerStrategy(String type, Supplier<FlowGraphBuildingStrategy> factory) {\n\t\tif (type == null || type.trim().isEmpty()) {\n\t\t\tthrow new IllegalArgumentException(\"Strategy type cannot be null or empty\");\n\t\t}\n\t\tif (factory == null) {\n\t\t\tthrow new IllegalArgumentException(\"Strategy factory cannot be null\");\n\t\t}\n\t\tif (strategyFactories.containsKey(type)) {\n\t\t\tthrow new IllegalArgumentException(\"Strategy type '\" + type + \"' is already registered\");\n\t\t}\n\t\tstrategyFactories.put(type, factory);\n\t}\n\n\t/**\n\t * Creates a new strategy instance by type. For built-in strategies a new instance is\n\t * returned each time; for strategies registered via {@link #registerStrategy(FlowGraphBuildingStrategy)}\n\t * the same instance is returned.\n\t * @param type the strategy type\n\t * @return a strategy implementation\n\t * @throws IllegalArgumentException if no strategy is found for the type\n\t */\n\tpublic FlowGraphBuildingStrategy createStrategy(String type) {\n\t\tif (type == null || type.trim().isEmpty()) {\n\t\t\tthrow new IllegalArgumentException(\"Strategy type cannot be null or empty\");","sourceCodeStart":69,"sourceCodeEnd":105,"githubUrl":"https://github.com/alibaba/spring-ai-alibaba/blob/f82da0b50f35744c13968191be2b1cd2452ef550/spring-ai-alibaba-agent-framework/src/main/java/com/alibaba/cloud/ai/graph/agent/flow/strategy/FlowGraphBuildingStrategyRegistry.java#L69-L105","documentation":"Thrown by FlowGraphBuildingStrategyRegistry.registerStrategy(String, Supplier) when the factory supplier argument is null. The registry stores the supplier and calls it later in createStrategy; a null supplier would cause an unusable registration, so it is rejected immediately. Fail-fast validation to keep the registry internally consistent.","triggerScenarios":"registerStrategy(\"loop\", null); passing a method reference/lambda variable that was never assigned; a factory field initialized conditionally and left null.","commonSituations":"Optional factory beans that did not resolve to a bean (null injection); conditional wiring code paths skipping factory creation; test code passing null to satisfy a signature.","solutions":["Pass a valid supplier, e.g. registry.registerStrategy(\"loop\", LoopGraphBuildingStrategy::new) or () -> new MyStrategy().","Ensure the factory bean/field is initialized before registration.","Guard with Objects.requireNonNull(factory) at the call site to fail with a clearer message."],"exampleFix":"// before\nSupplier<FlowGraphBuildingStrategy> factory = null;\nregistry.registerStrategy(\"loop\", factory);\n// after\nSupplier<FlowGraphBuildingStrategy> factory = LoopGraphBuildingStrategy::new;\nregistry.registerStrategy(\"loop\", factory);","handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":["java","illegal-argument","null-argument","registry"],"backgroundTag":"null-argument","analyzedSha":"f82da0b50f35744c13968191be2b1cd2452ef550","analyzedAt":"2026-09-09T15:32:42.421Z","contentChangedAt":"2026-09-09T15:32:42.421Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}