karatelabs/karate · error · IllegalArgumentException

valueSupplier cannot be null

Error message

valueSupplier cannot be null

What it means

KarateSetBuilder's constructor validates its inputs eagerly: a non-null, non-blank key and a non-null valueSupplier Function<Session, Object> are required to build a Gatling feeder/set. The library throws IllegalArgumentException at construction time rather than failing later during simulation setup, so misconfiguration surfaces immediately.

Solutions

  1. Pass a non-null lambda or method reference as valueSupplier, e.g. session -> karateRun(feature)
  2. If the supplier is optional in your code, substitute a default such as session -> Collections.emptyMap()
  3. Check the key argument too — the same constructor rejects null/blank keys first

Example fix

// before
Function<Session, Object> supplier = config.isPerf() ? this::runKarate : null;
new KarateSetBuilder("users", supplier); // throws
// after
Function<Session, Object> supplier = config.isPerf() ? this::runKarate : session -> Collections.emptyMap();
new KarateSetBuilder("users", supplier);
Defensive patterns

Strategy: validation

Validate before calling

if (key == null || key.isBlank()) throw new IllegalArgumentException("key required");
if (valueSupplier == null) throw new IllegalArgumentException("valueSupplier required");
new KarateSetBuilder(key, valueSupplier);

Type guard

boolean ready(String k, Function<Session, Object> s) { return k != null && !k.isBlank() && s != null; }

Try / catch

try { new KarateSetBuilder(key, supplier); } catch (IllegalArgumentException e) { throw new IllegalStateException("bad KarateSetBuilder config: " + e.getMessage(), e); }

Prevention

When it happens

Trigger: Calling new KarateSetBuilder(key, null) — e.g. passing a method reference or lambda variable that is null because it was conditionally assigned, or refactoring code that previously passed a lambda and now passes a nullable supplier reference.

Common situations: Gatling scenario setup code where the Karate feature runner or supplier is built behind an if/else and one branch leaves the supplier null; copying example code and forgetting to supply the lambda; wiring the supplier from optional config that is absent.

Related errors


AI-assisted analysis of karatelabs/karate@a22eb90246 (2026-09-12). Data as JSON: /api/errors/4faa3f59e738481a. Report an issue: GitHub.

Appendix: source

Thrown at karate-gatling/src/main/java/io/karatelabs/gatling/KarateSetBuilder.java:59

 * </pre>
 */
public final class KarateSetBuilder implements ActionBuilder {

    private final String key;
    private final Function<Session, Object> valueSupplier;

    /**
     * Create a builder that sets a session variable.
     *
     * @param key the variable name
     * @param valueSupplier function to compute the value from the session
     */
    public KarateSetBuilder(String key, Function<Session, Object> valueSupplier) {
        if (key == null || key.isBlank()) {
            throw new IllegalArgumentException("key cannot be null or blank");
        }
        if (valueSupplier == null) {
            throw new IllegalArgumentException("valueSupplier cannot be null");
        }
        this.key = key;
        this.valueSupplier = valueSupplier;
    }

    /**
     * Convert to a session function for use with Gatling's exec().
     */
    public Function<Session, Session> toSessionFunction() {
        KarateSetAction action = new KarateSetAction(key, valueSupplier);
        return action.toSessionFunction();
    }

    @Override
    public io.gatling.core.action.builder.ActionBuilder asScala() {
        // Use Gatling's session hook approach
        Function<Session, Session> sessionFunc = toSessionFunction();
        // Convert Java function to Scala function that returns Validation[Session]

View on GitHub (pinned to a22eb90246)