{"record":{"id":"29e1b4258fb47d64","repo":"apache/cassandra","slug":"null-is-not-supported-inside-collections-29e1b4","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/SetType.java","lineNumber":283,"sourceCode":"    @Override\n    public List<ByteBuffer> filterSortAndValidateElements(List<ByteBuffer> buffers)\n    {\n        return filterSortAndValidateElements(buffers, ByteBufferAccessor.instance, elements.comparatorSet.buffer);\n    }\n\n    @Override\n    public List<byte[]> filterSortAndValidateElementsFromArrays(List<byte[]> buffers)\n    {\n        return filterSortAndValidateElements(buffers, ByteArrayAccessor.instance, elements.comparatorSet.array);\n    }\n\n    private <V> List<V> filterSortAndValidateElements(List<V> buffers, ValueAccessor<V> valueAccessor, Comparator<V> comparator)\n    {\n        SortedSet<V> sorted = new TreeSet<>(comparator);\n        for (V buffer: buffers)\n        {\n            if (buffer == null)\n                throw new MarshalException(\"null is not supported inside collections\");\n            elements.validate(buffer, valueAccessor);\n            sorted.add(buffer);\n        }\n        return new ArrayList<>(sorted);\n    }\n\n    @Override\n    protected int compareNextCell(Iterator<Cell<?>> cellIterator, Iterator<ByteBuffer> elementIter)\n    {\n        return getElementsType().compare(cellIterator.next().path().get(0), elementIter.next());\n    }\n\n    @Override\n    public boolean contains(ComplexColumnData columnData, ByteBuffer value)\n    {\n        return columnData.getCell(CellPath.create(value)) != null;\n    }\n}","sourceCodeStart":265,"sourceCodeEnd":301,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/db/marshal/SetType.java#L265-L301","documentation":"SetType validates every element buffer supplied when constructing a set value. Cassandra sets cannot contain null elements, so if any buffer in the incoming list is null the type throws this MarshalException before sorting/validating elements. It protects the internal sorted-set representation from nulls.","triggerScenarios":"Calling SetType.decompose / builder (e.g. SetType.getInstance(...).decompose or ListBox builder) with a list of ByteBuffer elements that contains a null entry; binding a CQL literal or bound value for a set column where one element is null (e.g. SET c = {1, null, 3}).","commonSituations":"Application code deserializing data from another source that represents missing values as null and passing them straight into a set builder; drivers/JSON-to-CQL converters that do not filter nulls before building collection values.","solutions":["Filter null elements out of the collection before passing it to SetType (builder.add / decompose).","Decide the desired semantics: skip nulls, or reject the record upstream with a clearer application-level error.","Check any JSON/JSONB deserialization path that maps missing fields to null elements and drop them instead."],"exampleFix":"// before\nList<ByteBuffer> elements = jsonList.stream().map(this::toByteBuffer).collect(toList());\nset.decompose(elements);\n// after\nList<ByteBuffer> elements = jsonList.stream().filter(Objects::nonNull).map(this::toByteBuffer).collect(toList());\nset.decompose(elements);","handlingStrategy":"validation","validationCode":"List<ByteBuffer> safe = elements == null ? Collections.emptyList() : elements.stream().filter(Objects::nonNull).collect(Collectors.toList());","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never put null elements into collection builders; filter or map them to defaults first","Review any JSON/ORM layer that maps missing fields to null collection entries","Unit-test collection serialization with payloads containing missing values"],"tags":["cassandra","serialization","collections","null-value"],"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"}