{"record":{"id":"5a0c3c4c0ab88630","repo":"apache/cassandra","slug":"invalid-null-key-in-map","errorCode":null,"errorMessage":"Invalid null key in map","messagePattern":"Invalid null key in map","errorType":"validation","errorClass":"MarshalException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/db/marshal/MapType.java","lineNumber":347,"sourceCode":"        return bbs;\n    }\n\n    @Override\n    public Term fromJSONObject(Object parsed) throws MarshalException\n    {\n        if (parsed instanceof String)\n            parsed = JsonUtils.decodeJson((String) parsed);\n\n        if (!(parsed instanceof Map))\n            throw new MarshalException(String.format(\n                    \"Expected a map, but got a %s: %s\", parsed.getClass().getSimpleName(), parsed));\n\n        Map<?, ?> map = (Map<?, ?>) parsed;\n        List<Term> terms = new ArrayList<>(map.size() << 1);\n        for (Map.Entry<?, ?> entry : map.entrySet())\n        {\n            if (entry.getKey() == null)\n                throw new MarshalException(\"Invalid null key in map\");\n\n            if (entry.getValue() == null)\n                throw new MarshalException(\"Invalid null value in map\");\n\n            terms.add(keys.fromJSONObject(entry.getKey()));\n            terms.add(values.fromJSONObject(entry.getValue()));\n        }\n        return new MultiElements.DelayedValue(this, terms);\n    }\n\n    @Override\n    public String toJSONString(ByteBuffer buffer, ProtocolVersion protocolVersion)\n    {\n        ByteBuffer value = buffer.duplicate();\n        StringBuilder sb = new StringBuilder(\"{\");\n        int size = CollectionSerializer.readCollectionSize(value, ByteBufferAccessor.instance);\n        int offset = CollectionSerializer.sizeOfCollectionSize();\n        for (int i = 0; i < size; i++)","sourceCodeStart":329,"sourceCodeEnd":365,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/db/marshal/MapType.java#L329-L365","documentation":"When building map Terms from JSON, MapType.fromJSONObject() iterates the decoded Map's entries and rejects any entry whose key is null. JSON decoding can yield null keys (e.g. JSON null key or a decoded map containing a null key), and Cassandra maps cannot contain null keys, so MarshalException is thrown.","triggerScenarios":"Calling fromJSONObject on MapType with a Map that contains a null key — typically from JSON like {\"m\": {\"null\": null}} decoded with a null key entry, or programmatically constructed maps with null keys.","commonSituations":"JSON ingestion pipelines producing maps with null keys; client libraries emitting {'key': null} entries; deserializers that map JSON null placeholders to Java null keys.","solutions":["Remove null-key entries from the JSON object before sending: every key must be a non-null value.","Sanitize the decoded Map programmatically (filter entries with null keys) before calling fromJSONObject.","Catch MarshalException and report which map field contains a null key to the caller."],"exampleFix":"// before\n{\"m\": {\"a\": 1, null: 2}}\n// after\n{\"m\": {\"a\": 1, \"b\": 2}}","handlingStrategy":"validation","validationCode":"Map<?,?> map = (Map<?,?>) decoded;\nif (map.keySet().stream().anyMatch(Objects::isNull))\n    throw new IllegalArgumentException(\"map contains a null key\");","typeGuard":null,"tryCatchPattern":"try {\n    Term term = mapType.fromJSONObject(parsed);\n} catch (MarshalException e) {\n    if (e.getMessage().contains(\"null key\")) {\n        // strip null-key entries or reject record\n    }\n}","preventionTips":["Never emit entries with null keys in JSON objects","Sanitize decoded maps: entries.removeIf(e -> e.getKey() == null)","Use explicit sentinel keys instead of null placeholders in upstream data"],"tags":["cassandra","json","map","null"],"backgroundTag":"null-argument","analyzedSha":"88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1","analyzedAt":"2026-09-10T07:29:22.284Z","contentChangedAt":"2026-09-10T07:29:22.284Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}