{"record":{"id":"4faa3f59e738481a","repo":"karatelabs/karate","slug":"valuesupplier-cannot-be-null","errorCode":null,"errorMessage":"valueSupplier cannot be null","messagePattern":"valueSupplier cannot be null","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"karate-gatling/src/main/java/io/karatelabs/gatling/KarateSetBuilder.java","lineNumber":59,"sourceCode":" * </pre>\n */\npublic final class KarateSetBuilder implements ActionBuilder {\n\n    private final String key;\n    private final Function<Session, Object> valueSupplier;\n\n    /**\n     * Create a builder that sets a session variable.\n     *\n     * @param key the variable name\n     * @param valueSupplier function to compute the value from the session\n     */\n    public KarateSetBuilder(String key, Function<Session, Object> valueSupplier) {\n        if (key == null || key.isBlank()) {\n            throw new IllegalArgumentException(\"key cannot be null or blank\");\n        }\n        if (valueSupplier == null) {\n            throw new IllegalArgumentException(\"valueSupplier cannot be null\");\n        }\n        this.key = key;\n        this.valueSupplier = valueSupplier;\n    }\n\n    /**\n     * Convert to a session function for use with Gatling's exec().\n     */\n    public Function<Session, Session> toSessionFunction() {\n        KarateSetAction action = new KarateSetAction(key, valueSupplier);\n        return action.toSessionFunction();\n    }\n\n    @Override\n    public io.gatling.core.action.builder.ActionBuilder asScala() {\n        // Use Gatling's session hook approach\n        Function<Session, Session> sessionFunc = toSessionFunction();\n        // Convert Java function to Scala function that returns Validation[Session]","sourceCodeStart":41,"sourceCodeEnd":77,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-gatling/src/main/java/io/karatelabs/gatling/KarateSetBuilder.java#L41-L77","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Pass a non-null lambda or method reference as valueSupplier, e.g. session -> karateRun(feature)","If the supplier is optional in your code, substitute a default such as session -> Collections.emptyMap()","Check the key argument too — the same constructor rejects null/blank keys first"],"exampleFix":"// before\nFunction<Session, Object> supplier = config.isPerf() ? this::runKarate : null;\nnew KarateSetBuilder(\"users\", supplier); // throws\n// after\nFunction<Session, Object> supplier = config.isPerf() ? this::runKarate : session -> Collections.emptyMap();\nnew KarateSetBuilder(\"users\", supplier);","handlingStrategy":"validation","validationCode":"if (key == null || key.isBlank()) throw new IllegalArgumentException(\"key required\");\nif (valueSupplier == null) throw new IllegalArgumentException(\"valueSupplier required\");\nnew KarateSetBuilder(key, valueSupplier);","typeGuard":"boolean ready(String k, Function<Session, Object> s) { return k != null && !k.isBlank() && s != null; }","tryCatchPattern":"try { new KarateSetBuilder(key, supplier); } catch (IllegalArgumentException e) { throw new IllegalStateException(\"bad KarateSetBuilder config: \" + e.getMessage(), e); }","preventionTips":["Never assign a supplier lambda to a nullable variable — build it inline in the constructor call","Initialize suppliers as final fields at construction time","Fail fast in your own config-loading code before reaching the builder"],"tags":["java","gatling","null-argument","constructor"],"backgroundTag":"invalid-constructor-argument","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-19T12:17:13.211Z"}