{"record":{"id":"0c044e2e7f4aac89","repo":"apache/cassandra","slug":"null-is-not-supported-inside-collections-0c044e","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/MapType.java","lineNumber":422,"sourceCode":"\n    @Override\n    public List<byte[]> filterSortAndValidateElementsFromArrays(List<byte[]> buffers)\n    {\n        return filterSortAndValidateElements(buffers, ByteArrayAccessor.instance, getKeysType().comparatorSet.array);\n    }\n\n    private <T> List<T> filterSortAndValidateElements(List<T> buffers, ValueAccessor<T> valueAccessor, Comparator<T> comparator)\n    {\n        // We depend on Maps to be properly sorted by their keys, so use a sorted map implementation here.\n        SortedMap<T, T> map = new TreeMap<>(comparator);\n        Iterator<T> iter = buffers.iterator();\n        while (iter.hasNext())\n        {\n            T keyBytes = iter.next();\n            T valueBytes = iter.next();\n\n            if (keyBytes == null || valueBytes == null)\n                throw new MarshalException(\"null is not supported inside collections\");\n\n            getKeysType().validate(keyBytes, valueAccessor);\n            getValuesType().validate(valueBytes, valueAccessor);\n\n            map.put(keyBytes, valueBytes);\n        }\n\n        List<T> sortedBuffers = new ArrayList<>(map.size() << 1);\n        for (Map.Entry<T, T> entry : map.entrySet())\n        {\n            sortedBuffers.add(entry.getKey());\n            sortedBuffers.add(entry.getValue());\n        }\n        return sortedBuffers;\n    }\n\n    protected int compareNextCell(Iterator<Cell<?>> cellIterator, Iterator<ByteBuffer> elementIter)\n    {","sourceCodeStart":404,"sourceCodeEnd":440,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/db/marshal/MapType.java#L404-L440","documentation":"MapType.filterSortAndValidateElements() validates serialized map element pairs: it walks the serialized buffer in key/value pairs and throws MarshalException if any key or value byte slice is null. Null elements are not representable or permitted inside Cassandra collections.","triggerScenarios":"Calling filterSortAndValidateElements (or filterSortAndValidateElementsFromArrays) with serialized map data containing null key or value buffers — typically from bind values where a map entry's key or value is null.","commonSituations":"Prepared-statement binds with Java maps containing null keys/values; protocol-level collection serialization bugs; client drivers encoding nulls into collection payloads.","solutions":["Ensure map values bound to collection columns contain no null keys or null values; filter them out client-side before binding.","Use a separate DELETE statement to remove entries rather than binding null values.","Catch MarshalException around statement binding/execution and sanitize the collection input."],"exampleFix":"// before\nmap.put(1, null); // bound to map<int,text> column\n// after\nmap.remove(1); // or provide a non-null value","handlingStrategy":"validation","validationCode":"for (Map.Entry<K,V> e : boundMap.entrySet()) {\n    if (e.getKey() == null || e.getValue() == null)\n        throw new IllegalArgumentException(\"collection bind values must not contain null keys/values\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    statement.bind(map);\n} catch (MarshalException e) {\n    if (e.getMessage().contains(\"null is not supported inside collections\")) {\n        // sanitize map and rebind\n    }\n}","preventionTips":["Filter null keys/values from Java maps before binding to collection columns","Model removal with DELETE statements, not null values","Add unit tests for bind payloads containing nulls"],"tags":["cassandra","map","null","serialization"],"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"}