{"record":{"id":"419757c61e5815f7","repo":"karatelabs/karate","slug":"called-on-null-or-undefined","errorCode":null,"errorMessage":" called on null or undefined","messagePattern":" called on null or undefined","errorType":"exception","errorClass":"JsErrorException","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/js/JsObjectConstructor.java","lineNumber":218,"sourceCode":"    private Object entries(Context context, Object[] args) {\n        requireObjectCoercible(args, \"Object.entries\");\n        CoreContext cc = context instanceof CoreContext c ? c : null;\n        List<Object> result = new ArrayList<>();\n        for (KeyValue kv : Terms.toIterable(args[0], cc)) {\n            List<Object> entry = new ArrayList<>();\n            entry.add(kv.key());\n            entry.add(kv.value());\n            result.add(new JsArray(entry));\n        }\n        return new JsArray(result);\n    }\n\n    /** Spec ToObject preamble: Object.keys/values/entries throw TypeError on\n     *  null/undefined ahead of any iteration (test262\n     *  Object/keys/15.2.3.14-1-4 and -1-5). */\n    private static void requireObjectCoercible(Object[] args, String op) {\n        if (args.length < 1 || args[0] == null || args[0] == Terms.UNDEFINED) {\n            throw JsErrorException.typeError(op + \" called on null or undefined\");\n        }\n    }\n\n    private Object assign(Context context, Object[] args) {\n        if (args.length == 0) {\n            return new LinkedHashMap<>();\n        }\n        if (args[0] == null || args[0] == Terms.UNDEFINED) {\n            throw JsErrorException.typeError(\"Cannot convert undefined or null to object\");\n        }\n        CoreContext cc = context instanceof CoreContext c ? c : null;\n        // §20.1.2.1: the target itself is mutated and returned — identity is\n        // observable (`Object.assign(t, src) === t`), and step 4c is\n        // Set(to, key, value, true), so target setters fire and a rejected\n        // write throws. Spread/rest keep CreateDataProperty semantics via\n        // Terms.copyDataProperties; the two operations are not the same seam.\n        // ToObject approximation for a primitive target: no mutable wrapper\n        // exists, so copy into a fresh map that stands in for the wrapper —","sourceCodeStart":200,"sourceCodeEnd":236,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/js/JsObjectConstructor.java#L200-L236","documentation":"Karate's JS engine implements the ECMAScript spec preamble for Object.keys/values/entries: they require a coercible object. Calling any of them with null, undefined, or no argument throws a TypeError instead of returning an empty result, matching test262 behavior.","triggerScenarios":"Object.keys(null), Object.values(undefined), Object.entries() with no args, or any of these called on a variable that resolved to null/undefined at runtime (e.g. a missing JSON response field).","commonSituations":"Iterating over an API response field that is absent; a Karate variable not yet defined; a function that returns null on failure being passed straight into Object.keys.","solutions":["Check the value is non-null before calling Object.keys/values/entries: `if (val) Object.keys(val)`.","Use a default: `Object.keys(val || {})`.","Fix upstream logic so the variable is defined before this call (e.g. define it in karate-config.js or guard with karate.get)."],"exampleFix":"// before\nvar names = Object.keys(response.body.user);\n// after\nvar names = Object.keys(response.body.user || {});","handlingStrategy":"type-guard","validationCode":"function isCoercibleObject(v) { return v != null; }\nif (!isCoercibleObject(val)) throw new Error('expected object for Object.keys');","typeGuard":"function isObjectLike(v) { return v !== null && typeof v === 'object'; }","tryCatchPattern":"try { var keys = Object.keys(val); } catch (e) { if (String(e).indexOf('called on null or undefined') !== -1) { var keys = []; } else { throw e; } }","preventionTips":["Default optional objects with `|| {}` before introspection","Define Karate variables before use (karate-config.js or Background)","Check API response fields exist before iterating them"],"tags":["javascript","typeerror","null-object"],"backgroundTag":"null-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"}