{"record":{"id":"4fd9e977c0aa6110","repo":"apache/cassandra","slug":"null-value-read-when-not-allowed","errorCode":null,"errorMessage":"Null value read when not allowed","messagePattern":"Null value read when not allowed","errorType":"validation","errorClass":"MarshalException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/serializers/CollectionSerializer.java","lineNumber":181,"sourceCode":"\n        output.putInt(accessor.size(value));\n        accessor.write(value, output);\n    }\n\n    public static <V> V readValue(V input, ValueAccessor<V> accessor, int offset)\n    {\n        int size = accessor.getInt(input, offset);\n        if (size < 0)\n            return null;\n\n        return accessor.slice(input, offset + TypeSizes.INT_SIZE, size);\n    }\n\n    public static <V> V readNonNullValue(V input, ValueAccessor<V> accessor, int offset)\n    {\n        V value = readValue(input, accessor, offset);\n        if (value == null)\n            throw new MarshalException(\"Null value read when not allowed\");\n        return value;\n    }\n\n    protected static void skipValue(ByteBuffer input)\n    {\n        int size = input.getInt();\n        input.position(input.position() + size);\n    }\n\n    public static <V> int skipValue(V input, ValueAccessor<V> accessor, int offset)\n    {\n        int size = accessor.getInt(input, offset);\n        return TypeSizes.sizeof(size) + size;\n    }\n\n    public static <V> int sizeOfValue(V value, ValueAccessor<V> accessor)\n    {\n        return value == null ? 4 : 4 + accessor.size(value);","sourceCodeStart":163,"sourceCodeEnd":199,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/serializers/CollectionSerializer.java#L163-L199","documentation":"readNonNullValue reads one collection element and rejects nulls. Collection elements are length-prefixed with -1 encoding null; collections in Cassandra do not permit null elements, so a null element read is treated as invalid data and MarshalException is thrown.","triggerScenarios":"Iterating a collection's elements where an element's length prefix is -1 — e.g. a map/list containing a null value inserted via a driver that allowed it, or bytes built manually with a null element marker.","commonSituations":"Legacy or corrupted data containing null collection members; custom serialization code writing -1 lengths; driver/UDF code inserting nulls into lists/sets/maps before validation was enforced.","solutions":["Remove or rewrite the offending null element in the data","In custom serialization, skip null elements instead of encoding them (use readValue and check for null)","Use fully-assembled collections in application code, never null members","If data is on disk, scrub/repair or migrate the affected rows"],"exampleFix":"// before\nV v = CollectionSerializer.readNonNullValue(input, accessor, offset); // throws on null\n// after\nV v = CollectionSerializer.readValue(input, accessor, offset);\nif (v == null) { v = defaultValue; }","handlingStrategy":"validation","validationCode":"public static boolean hasNullElements(ByteBuffer collectionBuf, AbstractType<?> elementType) {\n    ByteBuffer dup = collectionBuf.duplicate();\n    int n = dup.getInt();\n    for (int i = 0; i < n; i++) {\n        int size = dup.getInt();\n        if (size < 0) return true;\n        dup.position(dup.position() + size);\n    }\n    return false;\n}","typeGuard":"public static boolean isNonNullElement(ByteBuffer element) {\n    return element != null && element.remaining() >= 0;\n}","tryCatchPattern":"try {\n    serializer.validate(buffer, accessor);\n} catch (MarshalException e) {\n    logger.warn(\"Collection contains a null element: {}\", e.getMessage());\n}","preventionTips":["Never insert null elements into Cassandra lists/sets/maps","Filter nulls at the application layer before binding statements","Use write-time null filtering in drivers when building collection values"],"tags":["serialization","cassandra","null","collections"],"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"}