{"record":{"id":"720578e91713e153","repo":"karatelabs/karate","slug":"key-cannot-be-null-or-blank","errorCode":null,"errorMessage":"key cannot be null or blank","messagePattern":"key cannot be null or blank","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"karate-gatling/src/main/java/io/karatelabs/gatling/KarateSetBuilder.java","lineNumber":56,"sourceCode":" *     .feed(feeder)\n *     .exec(karateSet(\"userId\", s -&gt; s.getInt(\"id\")))\n *     .exec(karateFeature(\"classpath:features/user.feature\"))\n * </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() {","sourceCodeStart":38,"sourceCodeEnd":74,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-gatling/src/main/java/io/karatelabs/gatling/KarateSetBuilder.java#L38-L74","documentation":"KarateSetBuilder(key, valueSupplier) defines a Gatling session variable to set from Karate results. The variable name (key) is mandatory: null or blank keys are rejected with IllegalArgumentException before the supplier is validated.","triggerScenarios":"new KarateSetBuilder(null, fn) or new KarateSetBuilder(\"\", fn) / \" \", typically when the key is built dynamically from an empty config value or a typo leaves it unset.","commonSituations":"Reading the variable name from a properties file that lacks the entry; string concatenation producing an empty name; refactoring where the literal key was deleted.","solutions":["Pass a non-blank variable name, e.g. new KarateSetBuilder(\"tokenId\", session -> ...)","Validate the config/property supplying the key before constructing the builder","Also ensure valueSupplier is non-null — it is checked next"],"exampleFix":"// before\nString key = props.getProperty(\"set.key\"); // missing -> null\nnew KarateSetBuilder(key, s -> s.get(\"response\"));\n// after\nString key = props.getProperty(\"set.key\", \"tokenId\");\nnew KarateSetBuilder(key, s -> s.get(\"response\"));","handlingStrategy":"validation","validationCode":"if (key == null || key.isBlank()) { throw new IllegalArgumentException(\"set key must be non-blank\"); }\nif (supplier == null) { throw new IllegalArgumentException(\"supplier must be non-null\"); }","typeGuard":null,"tryCatchPattern":"try {\n  new KarateSetBuilder(key, supplier);\n} catch (IllegalArgumentException e) {\n  throw new IllegalStateException(\"Bad KarateSetBuilder config: \" + e.getMessage(), e);\n}","preventionTips":["Hardcode or default the variable name instead of reading it from optional config","Validate key and supplier together before constructing","Use Java's Objects.requireNonNull / isBlank checks upstream"],"tags":["gatling","karate","constructor","null","argument"],"backgroundTag":"null-argument","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-16T19:17:19.609Z"}