karatelabs/karate · error · JsErrorException

property key is null

Error message

property key is null

What it means

Object.defineProperty requires exactly three arguments: target, property key, and descriptor. When called with only a target (no property key argument), the engine throws this TypeError indicating the key argument is missing. The message is somewhat literal — it means no key argument was supplied at all.

Solutions

  1. Supply the property key as the second argument
  2. Check the call site / wrapper for dropped arguments when using spread or apply
  3. If the key may be absent, guard: if (key == null) handle before calling

Example fix

// before
Object.defineProperty(obj); // key missing
// after
Object.defineProperty(obj, 'x', {value: 1});
Defensive patterns

Strategy: validation

Validate before calling

if (key === undefined) throw new TypeError('property key required before defineProperty');

Type guard

function hasKeyArg(key) { return key !== undefined; }

Try / catch

try { Object.defineProperty(obj, key, desc); }
catch (e) { if (String(e).includes('property key is null')) { /* fix call arity — key argument was missing */ } else { throw e; } }

Prevention

When it happens

Trigger: Object.defineProperty(obj) or Object.defineProperty(obj, undefined-as-only-second-arg omitted) — i.e. args.length < 2. Passing explicitly undefined as the key is converted by ToPropertyKey and does NOT hit this path; only a missing second argument does.

Common situations: Partial application or a wrapper function that drops the key argument; spreading an array shorter than expected: defineProperty(obj, ...[desc]); typos calling a local defineProperty helper with wrong arity.

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/2c6f3cb8bae3d0c1. Report an issue: GitHub.

Appendix: source

Thrown at karate-js/src/main/java/io/karatelabs/js/JsObjectConstructor.java:553

        return new JsObject(desc);
    }

    /** Returns the own {@link AccessorSlot} at {@code key} when one exists,
     *  {@code null} otherwise (own data property, missing, or unsupported
     *  type). Scoped to own properties only — for the chain-walking variant
     *  see {@code PropertyAccess#findAccessorInChain}. */
    private static AccessorSlot ownAccessorSlot(Object obj, String key) {
        PropertySlot s = PropertyAccess.ownSlot(obj, key);
        return s instanceof AccessorSlot acc ? acc : null;
    }

    @SuppressWarnings("unchecked")
    Object defineProperty(Context context, Object[] args) {
        if (args.length < 1 || !(args[0] instanceof ObjectLike || args[0] instanceof Map)) {
            throw JsErrorException.typeError("Object.defineProperty called on non-object");
        }
        if (args.length < 2) {
            throw JsErrorException.typeError("property key is null");
        }
        if (args.length < 3 || args[2] == null || args[2] == Terms.UNDEFINED) {
            throw JsErrorException.typeError("Property descriptor must be an object");
        }
        // Spec ToPropertyKey: pass ctx so ObjectLike keys dispatch through
        // ToPrimitive(string) → ToString (test262 Object/defineProperty/
        // 15.2.3.6-2-{20,24,25,39,41,43,44,45,46,47,48} pass arrays / boxed
        // booleans / objects with overridden toString as the key arg).
        CoreContext keyCtx = context instanceof CoreContext c ? c : null;
        String prop = Terms.toPropertyKey(args[1], keyCtx);
        Object desc = args[2];
        ObjectLike descObj;
        Map<String, Object> descMap;
        if (desc instanceof ObjectLike ol) {
            descObj = ol;
            descMap = ol.toMap();
        } else if (desc instanceof Map) {
            descObj = null;

View on GitHub (pinned to a22eb90246)