{"record":{"id":"c95059af0e814918","repo":"apache/cassandra","slug":"invalid-type-for-s-element-expecting-s-but-got","errorCode":null,"errorMessage":"Invalid type for %s element, expecting %s but got %s","messagePattern":"Invalid type for (.+?) element, expecting (.+?) but got (.+?)","errorType":"exception","errorClass":"InvalidTypeException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/functions/types/TypeCodec.java","lineNumber":2132,"sourceCode":"        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        }\n\n        @Override\n        public C deserialize(ByteBuffer bytes, ProtocolVersion protocolVersion)\n        {\n            if (bytes == null || bytes.remaining() == 0) return newInstance(0);\n            try\n            {\n                ByteBuffer input = bytes.duplicate();\n                int size = CodecUtils.readSize(input, protocolVersion);","sourceCodeStart":2114,"sourceCodeEnd":2150,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/functions/types/TypeCodec.java#L2114-L2150","documentation":"Thrown by TypeCodec's collection codecs when serializing a collection/list/set/map element fails because the element object is not an instance of the Java type the element codec expects. The codec catches the resulting ClassCastException and rethrows it as an InvalidTypeException naming the CQL type, the expected Java type, and the actual element class. This is the Java-driver-side guard ensuring collection elements match the declared CQL type before writing bytes.","triggerScenarios":"Calling a collection codec's serialize() (e.g. via bound statement values, or TypeCodec<List<...>>.serialize directly) with a collection whose element is of the wrong Java class, e.g. putting an Integer into a List<String> column value with an unchecked cast, or a raw-typed collection after a CQL type change.","commonSituations":"Raw types or @SuppressWarnings casts hiding heterogeneous elements; schema changed the column's element type (int -> text) while code still builds old-typed collections; generic erasure letting Integer sneak into what was declared List<String>; using codec that doesn't match the actual statement metadata.","solutions":["Fix the collection construction so every element is an instance of eltCodec.getJavaType() (e.g. use String elements for a list<text> column)","Verify the column's CQL type in the schema and pick the matching codec via codecRegistry.codecFor(column.getType())","Enable generics properly (avoid raw types) so javac catches the mismatch at compile time","If the driver jar was rebuilt against a changed schema, recompile application code against the new types"],"exampleFix":"// before\nList raw = new ArrayList();\nraw.add(42); // column is list<text>\nboundStatement.setList(\"tags\", raw);\n// after\nList<String> tags = new ArrayList<>();\ntags.add(\"42\");\nboundStatement.setList(\"tags\", tags);","handlingStrategy":"type-guard","validationCode":"// before serializing\nClass<?> expected = codec.getJavaType().getRawType(); // for element codec: eltCodec.getJavaType()\nfor (Object elt : collection) {\n    if (elt != null && !expected.isInstance(elt))\n        throw new IllegalArgumentException(\"Element \" + elt + \" is not \" + expected.getName());\n}","typeGuard":"static <T> List<T> guardElements(Collection<?> c, Class<T> eltType) {\n    for (Object o : c)\n        if (o != null && !eltType.isInstance(o))\n            throw new ClassCastException(\"Expected \" + eltType + \" but got \" + o.getClass());\n    return (List<T>) c;\n}","tryCatchPattern":"try {\n    bb = codec.serialize(value, protocolVersion);\n} catch (InvalidTypeException e) {\n    LOG.error(\"Element type mismatch: {}\", e.getMessage(), e);\n    throw new IllegalArgumentException(\"Fix collection element types before binding\", e);\n}","preventionTips":["Always use parameterized generics (List<String>, not raw List)","Derive codecs from column metadata via CodecRegistry instead of hard-coding types","Recompile and run codec round-trip tests after any schema element-type change"],"tags":["cassandra","java-driver","serialization","type-mismatch"],"backgroundTag":"type-mismatch","analyzedSha":"88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1","analyzedAt":"2026-09-10T07:29:22.284Z","contentChangedAt":"2026-09-10T07:29:22.284Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}