{"record":{"id":"5c0008c40f082ce9","repo":"apache/cassandra","slug":"collection-elements-cannot-be-null","errorCode":null,"errorMessage":"Collection elements cannot be null","messagePattern":"Collection elements cannot be null","errorType":"exception","errorClass":"NullPointerException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/functions/types/TypeCodec.java","lineNumber":2123,"sourceCode":"            super(cqlType, javaType);\n            checkArgument(\n            cqlType.getName() == Name.LIST || cqlType.getName() == Name.SET,\n            \"Expecting list or set type, got %s\",\n            cqlType);\n            this.eltCodec = eltCodec;\n        }\n\n        @Override\n        public ByteBuffer serialize(C value, ProtocolVersion protocolVersion)\n        {\n            if (value == null) return null;\n            int i = 0;\n            ByteBuffer[] bbs = new ByteBuffer[value.size()];\n            for (E elt : value)\n            {\n                if (elt == null)\n                {\n                    throw new NullPointerException(\"Collection elements cannot be null\");\n                }\n                ByteBuffer bb;\n                try\n                {\n                    bb = eltCodec.serialize(elt, protocolVersion);\n                }\n                catch (ClassCastException e)\n                {\n                    throw new InvalidTypeException(\n                    String.format(\n                    \"Invalid type for %s element, expecting %s but got %s\",\n                    cqlType, eltCodec.getJavaType(), elt.getClass()),\n                    e);\n                }\n                bbs[i++] = bb;\n            }\n            return CodecUtils.pack(bbs, value.size(), protocolVersion);\n        }","sourceCodeStart":2105,"sourceCodeEnd":2141,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/functions/types/TypeCodec.java#L2105-L2141","documentation":"NullPointerException from the list/set codec's serialize: one of the elements in the Java collection to serialize is null. CQL lists and sets do not permit null elements (unlike the collection itself, which may be null), so serialization aborts at the offending element.","triggerScenarios":"Thrown at src/java/org/apache/cassandra/cql3/functions/types/TypeCodec.java:2123 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Filter null elements out of the collection before serializing","Represent absent elements by omitting them, or model nullability with a map (maps permit null-typed values via sentinels) or a UDT","Validate client-side data construction to never insert nulls into list/set fields"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"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"}