{"record":{"id":"474c7db7faf31d50","repo":"karatelabs/karate","slug":"array-prototype-called-on-null-or-undefined","errorCode":null,"errorMessage":"Array.prototype.* called on null or undefined","messagePattern":"Array\\.prototype\\.\\* called on null or undefined","errorType":"exception","errorClass":"JsErrorException","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/js/JsArrayPrototype.java","lineNumber":454,"sourceCode":"                : Terms.objectToNumber(lenObj);\n        if (ctx != null && ctx.isError()) return;\n        double d = n == null ? Double.NaN : n.doubleValue();\n        if (d + addCount > 9007199254740991.0) { // 2^53 - 1\n            throw JsErrorException.typeError(\"Invalid array length\");\n        }\n    }\n\n    /** Spec-shape {@code O = ? ToObject(this value)}. Returns the receiver\n     *  unchanged when it's already an {@link ObjectLike}; wraps raw Java\n     *  Lists / arrays via {@link Terms#toObjectLike} (so a Java\n     *  {@link java.util.ArrayList} surfaces as a {@link JsArray} sharing the\n     *  underlying list — mutations propagate). Throws TypeError for\n     *  {@code null} / {@code undefined} per spec — independent of strict\n     *  mode (the spec ToObject step in every Array.prototype.* method\n     *  rejects {@code null} / {@code undefined} directly). */\n    private static ObjectLike toReceiver(Object thisObj) {\n        if (thisObj == null || thisObj == Terms.UNDEFINED) {\n            throw JsErrorException.typeError(\"Array.prototype.* called on null or undefined\");\n        }\n        ObjectLike o = thisObj instanceof ObjectLike ol ? ol : Terms.toObjectLike(thisObj);\n        if (o == null) {\n            throw JsErrorException.typeError(\"Array.prototype.* called on null or undefined\");\n        }\n        return o;\n    }\n\n    private static final Object[] EMPTY_ARGS = new Object[0];\n\n    /** Spec {@code IsCallable} guard — every {@code Array.prototype.*}\n     *  iteration method begins with a TypeError when the supplied callbackfn\n     *  is not a function (spec FindViaPredicate / map / filter / forEach /\n     *  every / some / reduce / reduceRight / flatMap step 1). The method\n     *  name flows into the error so test262's\n     *  {@code Array.prototype.map called on non-callable} style assertions\n     *  carry the spec context. */\n    private static JsCallable requireCallable(Object[] args, String methodName) {","sourceCodeStart":436,"sourceCodeEnd":472,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/js/JsArrayPrototype.java#L436-L472","documentation":"Every Array.prototype.* method performs spec ToObject on its this value, which rejects null and undefined outright. Karate's toReceiver throws this TypeError when the receiver is null or Terms.UNDEFINED, independent of strict mode.","triggerScenarios":"Calling any Array.prototype method (e.g. via target/receiver helpers) with this set to null or undefined — e.g. Array.prototype.map.call(null, fn), or a function invoked unbound where this is undefined in strict mode.","commonSituations":"Detached method references like `const m = arr.map; m(fn)`; sloppy-to-strict ported code; variables that are null because a lookup returned nothing.","solutions":["Call the method on a real array or bound object: arr.map(fn)","Bind a receiver: Array.prototype.map.call(arr, fn)","Guard the receiver before the call: if (x == null) ...","Initialize the variable to [] instead of null/undefined"],"exampleFix":"// before\nconst map = arr.map;\nmap(fn); // this === undefined\n// after\narr.map(fn);\n// or\nconst map = arr.map.bind(arr);\nmap(fn);","handlingStrategy":"type-guard","validationCode":"if (receiver == null) throw new Error('receiver required for Array.prototype call');","typeGuard":"function isArrayReceiver(o) { return o != null && (Array.isArray(o) || typeof o === 'object'); }","tryCatchPattern":"try { return Array.prototype.map.call(recv, fn); } catch (e) { if (String(e.message).includes('null or undefined')) return []; throw e; }","preventionTips":["Bind detached method references to their original array","Initialize collection variables to [] rather than null","Avoid Array.prototype.*.call(null|undefined) patterns","Use optional chaining before invoking array methods"],"tags":["javascript","array","null","type-error"],"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"}