{"record":{"id":"95bd6173828e8276","repo":"apache/cassandra","slug":"invalid-null-value-for-counter-increment-decrement","errorCode":null,"errorMessage":"Invalid null value for counter increment/decrement","messagePattern":"Invalid null value for counter increment/decrement","errorType":"validation","errorClass":"InvalidRequestException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/terms/Constants.java","lineNumber":202,"sourceCode":"        }\n\n        public AbstractType<?> getExactTypeIfKnown(String keyspace)\n        {\n            return null;\n        }\n    }\n\n    // We don't have \"unset\" literal in the syntax, but it's used implicitely for JSON \"DEFAULT UNSET\" option\n    public static final UnsetLiteral UNSET_LITERAL = new UnsetLiteral();\n\n    public static final Value UNSET_VALUE = new Value(ByteBufferUtil.UNSET_BYTE_BUFFER);\n\n    private static class NullLiteral extends Term.Raw\n    {\n        public Term prepare(String keyspace, ColumnSpecification receiver) throws InvalidRequestException\n        {\n            if (!testAssignment(keyspace, receiver).isAssignable())\n                throw new InvalidRequestException(\"Invalid null value for counter increment/decrement\");\n\n            return NULL_VALUE;\n        }\n\n        public AssignmentTestable.TestResult testAssignment(String keyspace, ColumnSpecification receiver)\n        {\n            return receiver.type instanceof CounterColumnType\n                 ? AssignmentTestable.TestResult.NOT_ASSIGNABLE\n                 : AssignmentTestable.TestResult.WEAKLY_ASSIGNABLE;\n        }\n\n        public String getText()\n        {\n            return \"NULL\";\n        }\n\n        public AbstractType<?> getExactTypeIfKnown(String keyspace)\n        {","sourceCodeStart":184,"sourceCodeEnd":220,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/terms/Constants.java#L184-L220","documentation":"Thrown when a NULL literal (or a bind variable bound to null) is prepared as the value of a counter increment/decrement. Counters cannot be set to null; every counter mutation must supply a numeric delta, so the CQL layer rejects the assignment during statement preparation before execution.","triggerScenarios":"Issuing `UPDATE t SET c = c + null` or `SET c = c - null` on a counter column, or executing a prepared statement `UPDATE t SET c = c + ?` with a null bound to the marker.","commonSituations":"Application code builds increments from optional/nullable values and forgets a default; an ORM or driver binds null when a delta field is absent; refactors turn a literal increment into a nullable parameter.","solutions":["Ensure the bound value for the counter delta is a non-null long before executing","Coalesce null deltas to 0 in application code, or skip the counter update clause entirely when the delta is absent","If the intent is to reset the counter, remember counters cannot be set/reset to a value or null — delete the counter row/column instead"],"exampleFix":"// before\nLong delta = getDelta(); // may be null\nsession.execute(\"UPDATE counts SET c = c + ?\", delta);\n// after\nLong delta = getDelta();\nif (delta != null)\n    session.execute(\"UPDATE counts SET c = c + ?\", delta);","handlingStrategy":"validation","validationCode":"if (delta == null) throw new IllegalArgumentException(\"counter delta must not be null\");","typeGuard":null,"tryCatchPattern":"try { session.execute(stmt); } catch (InvalidQueryException e) { if (e.getMessage().contains(\"Invalid null value for counter\")) { /* supply default or skip */ } else throw e; }","preventionTips":["Default nullable deltas to 0 before binding","Skip counter clauses when no delta is present","Remember counters cannot be null-ed or reset; delete instead"],"tags":["cql","counter","null-value","validation"],"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"}