alibaba/spring-ai-alibaba · error · IllegalArgumentException

Strategy cannot be null

Error message

Strategy cannot be null

What it means

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.

Source

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

		registerDefaultStrategies();
	}

	/**
	 * Gets the singleton instance of the registry.
	 * @return the registry instance
	 */
	public static FlowGraphBuildingStrategyRegistry getInstance() {
		return INSTANCE;
	}

	/**
	 * Registers a new graph building strategy (same instance returned each time).
	 * @param strategy the strategy to register
	 * @throws IllegalArgumentException if strategy is null or type is already registered
	 */
	public void registerStrategy(FlowGraphBuildingStrategy strategy) {
		if (strategy == null) {
			throw new IllegalArgumentException("Strategy cannot be null");
		}

		String type = strategy.getStrategyType();
		if (type == null || type.trim().isEmpty()) {
			throw new IllegalArgumentException("Strategy type cannot be null or empty");
		}

		if (strategyFactories.containsKey(type)) {
			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

View on GitHub (pinned to f82da0b50f)

Solutions

  1. Pass a constructed strategy instance to registerStrategy
  2. Fix plugin/bean loading so missing strategies are skipped or fail earlier with a clear message
  3. Guard custom registration code with Objects.requireNonNull and a helpful message

Example fix

// before
registry.registerStrategy(configuredStrategyOrNull);
// after
if (configuredStrategy != null) {
    registry.registerStrategy(configuredStrategy);
}
Defensive patterns

Strategy: validation

Validate before calling

if (strategy == null) throw new IllegalArgumentException("Cannot register a null strategy");

Type guard

static void registerIfPresent(FlowGraphBuildingStrategyRegistry r, FlowGraphBuildingStrategy s) { if (s != null) r.registerStrategy(s); }

Try / catch

try { registry.registerStrategy(strategy); } catch (IllegalArgumentException e) { log.error("Strategy registration failed: {}", e.getMessage()); }

Prevention

When it happens

Trigger: Calling registerStrategy(null) directly, or via Spring/ServiceLoader wiring where a strategy bean failed to instantiate and null was registered.

Common situations: Optional bean injection yielding null; test code registering strategies from a list containing null; reflection-based plugin loading that silently produced null.

Related errors


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