{"record":{"id":"cd2d66d79e0d3574","repo":"alibaba/spring-ai-alibaba","slug":"strategy-cannot-be-null","errorCode":null,"errorMessage":"Strategy cannot be null","messagePattern":"Strategy 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":60,"sourceCode":"\t\tregisterDefaultStrategies();\n\t}\n\n\t/**\n\t * Gets the singleton instance of the registry.\n\t * @return the registry instance\n\t */\n\tpublic static FlowGraphBuildingStrategyRegistry getInstance() {\n\t\treturn INSTANCE;\n\t}\n\n\t/**\n\t * Registers a new graph building strategy (same instance returned each time).\n\t * @param strategy the strategy to register\n\t * @throws IllegalArgumentException if strategy is null or type is already registered\n\t */\n\tpublic void registerStrategy(FlowGraphBuildingStrategy strategy) {\n\t\tif (strategy == null) {\n\t\t\tthrow new IllegalArgumentException(\"Strategy cannot be null\");\n\t\t}\n\n\t\tString type = strategy.getStrategyType();\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\n\t\tif (strategyFactories.containsKey(type)) {\n\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","sourceCodeStart":42,"sourceCodeEnd":78,"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#L42-L78","documentation":"Programmer-error guard in FlowGraphBuildingStrategyRegistry.registerStrategy. The registry maps strategy types to singleton FlowGraphBuildingStrategy instances; a null strategy has no type to register and would corrupt the map, so IllegalArgumentException is thrown immediately. As documented on the method, the same exception also fires when the strategy's type is already registered. Callers must pass a non-null strategy with a unique type.","triggerScenarios":"Calling registerStrategy(null) directly, or via Spring/ServiceLoader wiring where a strategy bean failed to instantiate and null was registered.","commonSituations":"Optional bean injection yielding null; test code registering strategies from a list containing null; reflection-based plugin loading that silently produced null.","solutions":["Pass a constructed strategy instance to registerStrategy","Fix plugin/bean loading so missing strategies are skipped or fail earlier with a clear message","Guard custom registration code with Objects.requireNonNull and a helpful message"],"exampleFix":"// before\nregistry.registerStrategy(configuredStrategyOrNull);\n// after\nif (configuredStrategy != null) {\n    registry.registerStrategy(configuredStrategy);\n}","handlingStrategy":"validation","validationCode":"if (strategy == null) throw new IllegalArgumentException(\"Cannot register a null strategy\");","typeGuard":"static void registerIfPresent(FlowGraphBuildingStrategyRegistry r, FlowGraphBuildingStrategy s) { if (s != null) r.registerStrategy(s); }","tryCatchPattern":"try { registry.registerStrategy(strategy); } catch (IllegalArgumentException e) { log.error(\"Strategy registration failed: {}\", e.getMessage()); }","preventionTips":["Use Objects.requireNonNull in registration wrappers","Ensure plugin/bean loading fails loudly instead of yielding null","Skip null entries from ServiceLoader/Spring collections before registering"],"tags":["null-check","registry","plugin"],"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"}