karatelabs/karate · error · RuntimeException

range() needs at least two arguments: start and end

Error message

range() needs at least two arguments: start and end

What it means

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.

Solutions

  1. Pass both bounds: range(0, 10) or with a step range(0, 10, 2).
  2. Ensure both arguments are numbers; verify the variables are defined before the call.
  3. For a descending range give a negative step: range(5, 0, -1).

Example fix

// before
def nums = range(10)
// after
def nums = range(0, 10)
Defensive patterns

Strategy: validation

Validate before calling

def nums = (start != null && end != null) ? range(start, end) : []

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


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

Appendix: source

Thrown at karate-core/src/main/java/io/karatelabs/core/KarateJsUtils.java:414

                return StringUtils.formatJson(obj);
            } else if (obj instanceof Node) {
                return Xml.toString((Node) obj, true);
            } else {
                return obj != null ? obj.toString() : "null";
            }
        };
    }

    /**
     * Generate a range of integers.
     * Usage: karate.range(0, 5) => [0, 1, 2, 3, 4]
     *        karate.range(0, 10, 2) => [0, 2, 4, 6, 8]
     *        karate.range(5, 0, -1) => [5, 4, 3, 2, 1]
     */
    static JavaInvokable range() {
        return args -> {
            if (args.length < 2) {
                throw new RuntimeException("range() needs at least two arguments: start and end");
            }
            int start = ((Number) args[0]).intValue();
            int end = ((Number) args[1]).intValue();
            int step = args.length > 2 ? ((Number) args[2]).intValue() : 1;
            List<Integer> result = new ArrayList<>();
            if (step > 0) {
                for (int i = start; i < end; i += step) {
                    result.add(i);
                }
            } else if (step < 0) {
                for (int i = start; i > end; i += step) {
                    result.add(i);
                }
            }
            return result;
        };
    }

View on GitHub (pinned to a22eb90246)