{"record":{"id":"adce314a95459597","repo":"karatelabs/karate","slug":"range-needs-at-least-two-arguments-start-and-end","errorCode":null,"errorMessage":"range() needs at least two arguments: start and end","messagePattern":"range\\(\\) needs at least two arguments: start and end","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/core/KarateJsUtils.java","lineNumber":414,"sourceCode":"                return StringUtils.formatJson(obj);\n            } else if (obj instanceof Node) {\n                return Xml.toString((Node) obj, true);\n            } else {\n                return obj != null ? obj.toString() : \"null\";\n            }\n        };\n    }\n\n    /**\n     * Generate a range of integers.\n     * Usage: karate.range(0, 5) => [0, 1, 2, 3, 4]\n     *        karate.range(0, 10, 2) => [0, 2, 4, 6, 8]\n     *        karate.range(5, 0, -1) => [5, 4, 3, 2, 1]\n     */\n    static JavaInvokable range() {\n        return args -> {\n            if (args.length < 2) {\n                throw new RuntimeException(\"range() needs at least two arguments: start and end\");\n            }\n            int start = ((Number) args[0]).intValue();\n            int end = ((Number) args[1]).intValue();\n            int step = args.length > 2 ? ((Number) args[2]).intValue() : 1;\n            List<Integer> result = new ArrayList<>();\n            if (step > 0) {\n                for (int i = start; i < end; i += step) {\n                    result.add(i);\n                }\n            } else if (step < 0) {\n                for (int i = start; i > end; i += step) {\n                    result.add(i);\n                }\n            }\n            return result;\n        };\n    }\n","sourceCodeStart":396,"sourceCodeEnd":432,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/core/KarateJsUtils.java#L396-L432","documentation":"range() is a Karate JS utility that builds a List of integers from start to end with an optional step (e.g. range(0,10,2)). It throws this error when called with fewer than two arguments, since start and end are both required.","triggerScenarios":"karate.range() with zero or one argument, e.g. range(10) missing the start, or a null/undefined variable collapsing the call to one bound argument.","commonSituations":"Confusing range() with single-argument range APIs in other languages (Python-style range(n)); one of the two variables feeding the call was not defined.","solutions":["Pass both bounds: range(0, 10) or with a step range(0, 10, 2).","Ensure both arguments are numbers; verify the variables are defined before the call.","For a descending range give a negative step: range(5, 0, -1)."],"exampleFix":"// before\ndef nums = range(10)\n// after\ndef nums = range(0, 10)","handlingStrategy":"validation","validationCode":"def nums = (start != null && end != null) ? range(start, end) : []","typeGuard":null,"tryCatchPattern":null,"preventionTips":["range() requires start AND end (optionally step) — unlike single-arg range in other languages.","Both arguments must be numbers; define them before the call.","For descending ranges pass a negative step explicitly."],"tags":["karate","javascript","argument-count","lists"],"backgroundTag":"missing-required-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"}