{"record":{"id":"22a7ce85ee384077","repo":"apache/cassandra","slug":"null-is-not-supported-inside-collections","errorCode":null,"errorMessage":"null is not supported inside collections","messagePattern":"null is not supported inside collections","errorType":"validation","errorClass":"MarshalException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/db/marshal/ListType.java","lineNumber":286,"sourceCode":"    @Override\n    public void forEach(ByteBuffer input, Consumer<ByteBuffer> action)\n    {\n        serializer.forEach(input, action);\n    }\n\n    @Override\n    public ByteBuffer getMaskedValue()\n    {\n        return decompose(Collections.emptyList());\n    }\n\n    @Override\n    public List<ByteBuffer> filterSortAndValidateElements(List<ByteBuffer> buffers)\n    {\n        for (ByteBuffer buffer: buffers)\n        {\n            if (buffer == null)\n                throw new MarshalException(\"null is not supported inside collections\");\n            elements.validate(buffer);\n        }\n        return buffers;\n    }\n\n    @Override\n    public List<byte[]> filterSortAndValidateElementsFromArrays(List<byte[]> buffers)\n    {\n        for (byte[] buffer: buffers)\n        {\n            if (buffer == null)\n                throw new MarshalException(\"null is not supported inside collections\");\n            elements.validate(buffer, ByteArrayAccessor.instance);\n        }\n        return buffers;\n    }\n\n    @Override","sourceCodeStart":268,"sourceCodeEnd":304,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/db/marshal/ListType.java#L268-L304","documentation":"filterSortAndValidateElements checks each serialized element buffer of a list before it is stored. Cassandra forbids null elements inside collections; a null ByteBuffer triggers MarshalException with this message.","triggerScenarios":"Programmatic use of ListType (e.g. building collection values via the driver or internal APIs) passing a list of ByteBuffers that contains null; deserialization paths constructing lists with null entries.","commonSituations":"Custom serialization code that leaves slots null; interop layers mapping nullable language collections directly onto Cassandra lists; drivers or tools that don't pre-filter nulls.","solutions":["Filter out null buffers before calling filterSortAndValidateElements.","Use empty/serialized defaults instead of null where a placeholder is needed.","Validate the collection contents at the application boundary before writing."],"exampleFix":"// before\nList<ByteBuffer> bufs = rows.stream().map(r -> r.buf).collect(toList()); // may contain null\ntype.filterSortAndValidateElements(bufs);\n// after\nList<ByteBuffer> bufs = rows.stream().map(r -> r.buf).filter(Objects::nonNull).collect(toList());\ntype.filterSortAndValidateElements(bufs);","handlingStrategy":"validation","validationCode":"for (ByteBuffer b : buffers) { if (b == null) throw new IllegalArgumentException(\"null element buffer in list\"); }","typeGuard":"List<ByteBuffer> nonNull(List<ByteBuffer> in) { return in.stream().filter(Objects::nonNull).collect(Collectors.toList()); }","tryCatchPattern":"try { return listType.filterSortAndValidateElements(buffers); } catch (MarshalException e) { throw new DataConstraintException(\"List contains null elements\", e); }","preventionTips":["Never map nullable language collections directly onto Cassandra list values.","Serialize defaults (empty values) instead of leaving null slots.","Run filterSortAndValidateElements as a pre-write sanity check in tooling."],"tags":["collection","null","list","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-14T16:17:12.679Z"}