alibaba/spring-ai-alibaba · error · IllegalArgumentException

Strategy factory cannot be null

Error message

Strategy factory cannot be null

What it means

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.

Source

Thrown at spring-ai-alibaba-agent-framework/src/main/java/com/alibaba/cloud/ai/graph/agent/flow/strategy/FlowGraphBuildingStrategyRegistry.java:87

			throw new IllegalArgumentException("Strategy type '" + type + "' is already registered");
		}

		strategyFactories.put(type, () -> strategy);
	}

	/**
	 * Registers a strategy factory. Each call to {@link #createStrategy(String)} or
	 * {@link #getStrategy(String)} will use the factory to obtain a strategy instance.
	 * @param type the strategy type
	 * @param factory the factory that creates strategy instances
	 * @throws IllegalArgumentException if type or factory is null, or type is already registered
	 */
	public void registerStrategy(String type, Supplier<FlowGraphBuildingStrategy> factory) {
		if (type == null || type.trim().isEmpty()) {
			throw new IllegalArgumentException("Strategy type cannot be null or empty");
		}
		if (factory == null) {
			throw new IllegalArgumentException("Strategy factory cannot be null");
		}
		if (strategyFactories.containsKey(type)) {
			throw new IllegalArgumentException("Strategy type '" + type + "' is already registered");
		}
		strategyFactories.put(type, factory);
	}

	/**
	 * Creates a new strategy instance by type. For built-in strategies a new instance is
	 * returned each time; for strategies registered via {@link #registerStrategy(FlowGraphBuildingStrategy)}
	 * the same instance is returned.
	 * @param type the strategy type
	 * @return a strategy implementation
	 * @throws IllegalArgumentException if no strategy is found for the type
	 */
	public FlowGraphBuildingStrategy createStrategy(String type) {
		if (type == null || type.trim().isEmpty()) {
			throw new IllegalArgumentException("Strategy type cannot be null or empty");

View on GitHub (pinned to f82da0b50f)

Solutions

  1. Pass a valid supplier, e.g. registry.registerStrategy("loop", LoopGraphBuildingStrategy::new) or () -> new MyStrategy().
  2. Ensure the factory bean/field is initialized before registration.
  3. Guard with Objects.requireNonNull(factory) at the call site to fail with a clearer message.

Example fix

// before
Supplier<FlowGraphBuildingStrategy> factory = null;
registry.registerStrategy("loop", factory);
// after
Supplier<FlowGraphBuildingStrategy> factory = LoopGraphBuildingStrategy::new;
registry.registerStrategy("loop", factory);
Defensive patterns

Strategy: validation

When it happens

Trigger: registerStrategy("loop", null); passing a method reference/lambda variable that was never assigned; a factory field initialized conditionally and left null.

Common situations: 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.

Related errors


AI-assisted analysis of alibaba/spring-ai-alibaba@f82da0b50f (2026-09-09). Data as JSON: /api/errors/038f7719bfe14f0d. Report an issue: GitHub.