{"record":{"id":"69f4d81e4ed35a64","repo":"apache/cassandra","slug":"element-at-index-d-is-null-expected-type-s-gi","errorCode":null,"errorMessage":"Element at index %d is null (expected type %s); given %s","messagePattern":"Element at index (.+?) is null \\(expected type (.+?)\\); given (.+?)","errorType":"validation","errorClass":"MarshalException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/db/marshal/VectorType.java","lineNumber":373,"sourceCode":"    }\n\n    @Override\n    public String toString(boolean ignoreFreezing)\n    {\n        return getClass().getName() + TypeParser.stringifyVectorParameters(elementType, ignoreFreezing, dimension);\n    }\n\n    private void check(List<?> values)\n    {\n        if (values.size() != dimension)\n            throw new MarshalException(String.format(\"Required %d elements, but saw %d\", dimension, values.size()));\n\n        // This code base always works with a list that is RandomAccess, so can use .get to avoid allocation\n        for (int i = 0; i < dimension; i++)\n        {\n            Object value = values.get(i);\n            if (value == null || (value instanceof ByteBuffer && elementSerializer.isNull((ByteBuffer) value)))\n                throw new MarshalException(String.format(\"Element at index %d is null (expected type %s); given %s\", i, elementType.asCQL3Type(), values));\n        }\n    }\n\n    private <V> void checkConsumedFully(V buffer, ValueAccessor<V> accessor, int offset)\n    {\n        int remaining = accessor.sizeFromOffset(buffer, offset);\n        if (remaining > 0)\n            throw new MarshalException(\"Unexpected \" + remaining + \" extraneous bytes after \" + asCQL3Type() + \" value\");\n    }\n    \n    private static void rejectNullOrEmptyValue()\n    {\n        throw new MarshalException(\"Invalid empty vector value\");\n    }\n\n    @Override\n    public ByteBuffer getMaskedValue()\n    {","sourceCodeStart":355,"sourceCodeEnd":391,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/db/marshal/VectorType.java#L355-L391","documentation":"After the size check, check(List<?>) iterates the elements and throws MarshalException if any element is null (or a null-equivalent ByteBuffer), reporting the offending index and expected element type. Vectors cannot contain null elements.","triggerScenarios":"Executing a statement whose vector literal or term list contains a null element, e.g. [1, null, 3] for vector<int, 3>, or a bound parameter that resolves to null inside a MultiElements.DelayedValue.","commonSituations":"Bind variables left null in the driver before executing; queries built from partially-populated data structures; missing embedding values in ML pipelines.","solutions":["Ensure every element in the list is non-null before executing the statement","Default or skip null elements before composing the vector","Add a pre-execution validation that scans the list for nulls"],"exampleFix":"// before\nList<Integer> elems = Arrays.asList(1, null, 3);\nstmt = session.prepare(\"INSERT INTO t (v) VALUES (?)\")...bind(elems);\n// after\nelems.set(1, 0); // or reject the record\nstmt = session.prepare(\"INSERT INTO t (v) VALUES (?)\")...bind(elems);","handlingStrategy":"validation","validationCode":"for (int i = 0; i < values.size(); i++)\n    if (values.get(i) == null) throw new IllegalArgumentException(\"element \" + i + \" is null\");","typeGuard":null,"tryCatchPattern":"try { /* execute */ } catch (MarshalException e) { /* null element: sanitize and retry */ }","preventionTips":["Non-null-check every element before binding","Initialize vector slots with defaults instead of leaving null"],"tags":["cassandra","vector-type","null","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"}