karatelabs/karate · error · IllegalArgumentException

input must not be null

Error message

input must not be null

What it means

Json.of() is the entry point that converts an arbitrary object/string into a Json document wrapper; it rejects null outright because there is no meaningful Json document for null input. The throw is an IllegalArgumentException, a caller-contract violation rather than a data-format problem.

Solutions

  1. Check for null before calling Json.of and decide on a default (e.g. empty map/list or Json.array())
  2. Return an empty Json via Json.array() when input is legitimately absent
  3. Fix upstream logic that produced the null value

Example fix

// before
Json json = Json.of(body); // NPE risk if body == null
// after
Json json = body == null ? Json.array() : Json.of(body);
Defensive patterns

Strategy: type-guard

Validate before calling

if (input == null) { throw new IllegalStateException("Json.of input not initialized"); }

Type guard

// Java: explicit null check before call
boolean isUsable(Object o) { return o != null; }

Try / catch

try { Json json = Json.of(input); } catch (IllegalArgumentException e) { if ("input must not be null".equals(e.getMessage())) { json = Json.array(); } else { throw e; } }

Prevention

When it happens

Trigger: Calling Json.of(null) directly, or passing a variable that can be null (e.g. a missing response body or config value) into Json.of().

Common situations: Passing response.getBody() results that are null when the HTTP call failed; pipeline values that were never initialized.

Related errors


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

Appendix: source

Thrown at karate-core/src/main/java/io/karatelabs/common/Json.java:106

    private final boolean array;
    private final boolean object;
    private final String prefix;

    private String prefix(String path) {
        return path.charAt(0) == '$' ? path : prefix + path;
    }

    public static Json object() {
        return new Json(JsonPath.parse("{}"));
    }

    public static Json array() {
        return new Json(JsonPath.parse("[]"));
    }

    public static Json of(Object any) {
        if (any == null) {
            throw new IllegalArgumentException("input must not be null");
        }
        if (any instanceof String s) {
            if (s.isBlank()) {
                throw new IllegalArgumentException("input string must not be empty or blank");
            }
            return new Json(JsonPath.parse(parseLenient(s)));
        } else if (any instanceof List || any instanceof Map) {
            return new Json(JsonPath.parse(any));
        } else {
            String json = JSONValue.toJSONString(any);
            return new Json(JsonPath.parse(json));
        }
    }

    public static Object parseLenient(String json) {
        if (json == null || json.isBlank()) {
            throw new RuntimeException("invalid json: input is null or blank");
        }

View on GitHub (pinned to a22eb90246)