{"record":{"id":"b5beb132f69a0cf8","repo":"apache/cassandra","slug":"invalid-null-map-key","errorCode":null,"errorMessage":"Invalid null map key","messagePattern":"Invalid null map key","errorType":"validation","errorClass":"InvalidRequestException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/terms/Maps.java","lineNumber":291,"sourceCode":"        {\n            super(column, t);\n            this.k = k;\n        }\n\n        @Override\n        public void collectMarkerSpecification(VariableSpecifications boundNames, Object owner)\n        {\n            super.collectMarkerSpecification(boundNames, owner);\n            k.collectMarkerSpecification(boundNames, owner);\n        }\n\n        public void execute(DecoratedKey partitionKey, RowUpdateBuilder builder) throws InvalidRequestException\n        {\n            assert column.type.isMultiCell() : \"Attempted to set a value for a single key on a frozen map\";\n            ByteBuffer key = k.bindAndGet(builder);\n            ByteBuffer value = t.bindAndGet(builder);\n            if (key == null)\n                throw new InvalidRequestException(\"Invalid null map key\");\n            if (key == ByteBufferUtil.UNSET_BYTE_BUFFER)\n                throw new InvalidRequestException(\"Invalid unset map key\");\n\n            CellPath path = CellPath.create(key);\n\n            if (value == null)\n            {\n                builder.addTombstone(column, path);\n            }\n            else if (value != ByteBufferUtil.UNSET_BYTE_BUFFER)\n            {\n                builder.addCell(column, path, value);\n            }\n        }\n    }\n\n    public static class Putter extends Operation\n    {","sourceCodeStart":273,"sourceCodeEnd":309,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/terms/Maps.java#L273-L309","documentation":"Fired when executing a SET map[key] = ... operation on a non-frozen (multi-cell) map: the bound key term evaluated to null (e.g. 'SET m[null] = 1' or a bound variable left null). Null is not a valid map key, so the update is rejected with an InvalidRequestException before building the cell.","triggerScenarios":"Executing `UPDATE t SET m[?] = ? WHERE ...` where the key bind parameter is bound to null.","commonSituations":"Optional key fields in application code left null; ORM mapping null map keys; drivers deserializing missing key columns as null.","solutions":["Bind a non-null key value before executing.","Skip the statement or use a whole-map update when the key is absent.","Validate keys client-side and reject nulls early."],"exampleFix":"// before\nsession.execute(\"UPDATE t SET m[?] = ? WHERE k=?\", null, val, pk);\n// after\nif (mapKey != null)\n    session.execute(\"UPDATE t SET m[?] = ? WHERE k=?\", mapKey, val, pk);","handlingStrategy":"validation","validationCode":"if (mapKey == null) throw new IllegalArgumentException(\"map key must not be null\");","typeGuard":"boolean validMapKey(Object k) { return k != null && !(k instanceof ByteBuffer && k == ByteBufferUtil.UNSET_BYTE_BUFFER); }","tryCatchPattern":"try { session.execute(\"UPDATE t SET m[?] = ? WHERE k=?\", mapKey, val, pk); } catch (InvalidRequestException e) { if (e.getMessage().contains(\"Invalid null map key\")) { /* fix binding */ } else throw e; }","preventionTips":["Reject null keys at the API boundary","Skip map-entry writes when the key is missing","Avoid auto-mapping nullable fields to map keys"],"tags":["cql","map","null-value","invalid-request"],"backgroundTag":"null-argument","analyzedSha":"88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1","analyzedAt":"2026-09-10T07:29:22.284Z","contentChangedAt":"2026-09-10T07:29:22.284Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}