{"record":{"id":"1ff9a1abac34a389","repo":"apache/cassandra","slug":"map-values-cannot-be-null","errorCode":null,"errorMessage":"Map values cannot be null","messagePattern":"Map values cannot be null","errorType":"exception","errorClass":"NullPointerException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/functions/types/TypeCodec.java","lineNumber":2475,"sourceCode":"                    throw new NullPointerException(\"Map keys cannot be null\");\n                }\n                try\n                {\n                    bbk = keyCodec.serialize(key, protocolVersion);\n                }\n                catch (ClassCastException e)\n                {\n                    throw new InvalidTypeException(\n                    String.format(\n                    \"Invalid type for map key, expecting %s but got %s\",\n                    keyCodec.getJavaType(), key.getClass()),\n                    e);\n                }\n                ByteBuffer bbv;\n                V v = entry.getValue();\n                if (v == null)\n                {\n                    throw new NullPointerException(\"Map values cannot be null\");\n                }\n                try\n                {\n                    bbv = valueCodec.serialize(v, protocolVersion);\n                }\n                catch (ClassCastException e)\n                {\n                    throw new InvalidTypeException(\n                    String.format(\n                    \"Invalid type for map value, expecting %s but got %s\",\n                    valueCodec.getJavaType(), v.getClass()),\n                    e);\n                }\n                bbs[i++] = bbk;\n                bbs[i++] = bbv;\n            }\n            return CodecUtils.pack(bbs, value.size(), protocolVersion);\n        }","sourceCodeStart":2457,"sourceCodeEnd":2493,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/functions/types/TypeCodec.java#L2457-L2493","documentation":"Thrown by MapCodec.serialize() when a map entry has a null value. CQL does not allow null values inside map collections (a null would be indistinguishable from absence/tombstone semantics), so the codec rejects the map at serialization time.","triggerScenarios":"Calling MapCodec.serialize() with a Map containing null values, e.g. m.put(k, null), which HashMap permits but CQL map storage does not.","commonSituations":"Maps populated from JSON/CSV sources where missing fields became nulls; Optional-handling code that inserted null instead of skipping the entry; upstream data with unknown values.","solutions":["Remove null-valued entries before serializing (value.entrySet().removeIf(e -> e.getValue() == null))","Skip nulls when building the map instead of putting them","Replace nulls with explicit sentinels/default values if absence must be represented"],"exampleFix":"// before\nm.put(\"k\", null);\ncodec.serialize(m, version); // NullPointerException\n// after\nif (v != null) m.put(\"k\", v);\ncodec.serialize(m, version);","handlingStrategy":"validation","validationCode":"if (map.values().stream().anyMatch(Objects::isNull))\n    throw new IllegalArgumentException(\"Map contains null values\");\ncodec.serialize(map, protocolVersion);","typeGuard":null,"tryCatchPattern":"try {\n    codec.serialize(map, protocolVersion);\n} catch (NullPointerException e) {\n    if (e.getMessage() != null && e.getMessage().contains(\"Map values cannot be null\")) {\n        map.entrySet().removeIf(en -> en.getValue() == null);\n        return codec.serialize(map, protocolVersion);\n    }\n    throw e;\n}","preventionTips":["Skip null values when populating maps instead of putting them","Sanitize JSON/CSV inputs so missing fields don't become null entries","Validate map contents before binding statements or serializing"],"tags":["map","null","serialization","codec"],"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"}