{"record":{"id":"544bfa851aac1f2f","repo":"apache/cassandra","slug":"invalid-null-value-for-element-selection-on-colum","errorCode":null,"errorMessage":"Invalid null value for element selection on <columnName>","messagePattern":"Invalid null value for element selection on <columnName>","errorType":"validation","errorClass":"InvalidRequestException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/selection/ElementsSelector.java","lineNumber":154,"sourceCode":"     * is selected.\n     * @param type the type of the collection.\n     * @param key the element within the value represented by {@code factory} that is selected.\n     * @return the created factory.\n     */\n    public static Factory newElementFactory(String name, Selector.Factory factory, CollectionType<?> type, final Term key)\n    {\n        return new AbstractFactory(name, factory, type)\n        {\n            protected AbstractType<?> getReturnType()\n            {\n                return valueType(type);\n            }\n\n            public Selector newInstance(QueryOptions options) throws InvalidRequestException\n            {\n                ByteBuffer keyValue = key.bindAndGet(options);\n                if (keyValue == null)\n                    throw new InvalidRequestException(\"Invalid null value for element selection on \" + factory.getColumnName());\n                if (keyValue == ByteBufferUtil.UNSET_BYTE_BUFFER)\n                    throw new InvalidRequestException(\"Invalid unset value for element selection on \" + factory.getColumnName());\n                return new ElementSelector(factory.newInstance(options), keyValue);\n            }\n\n            public boolean areAllFetchedColumnsKnown()\n            {\n                // If we known all the fetched columns, it means that we don't have to wait execution to create\n                // the ColumnFilter (through addFetchedColumns below).\n                // That's the case if either there is no particular subselection\n                // to add, or if there is one but the selected key is terminal. In other words,\n                // we known all the fetched columns if all the feched columns of the factory are known and either:\n                //  1) the type is frozen (in which case there isn't subselection to do).\n                //  2) the factory (the left-hand-side) isn't a simple column selection (here again, no\n                //     subselection we can do).\n                //  3) the element selected is terminal.\n                return factory.areAllFetchedColumnsKnown()\n                        && (!type.isMultiCell() || !factory.isSimpleSelectorFactory() || key.isTerminal());","sourceCodeStart":136,"sourceCodeEnd":172,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/selection/ElementsSelector.java#L136-L172","documentation":"For collection element selection (e.g. col[key] on a map/list/set column), the bound key value must be non-null; bindAndGet returning null (null literal or null bound variable) is rejected. (An UNSET marker is separately rejected with a sibling message.)","triggerScenarios":"A SELECT ... FROM t WHERE / selection using m[?] or m[null] where the marker is bound to null in QueryOptions, i.e. a null or missing bound value for the element-selection key.","commonSituations":"Driver statements with unset/null parameters for the map key in element selection; also literal 'm[null]' expressions written by mistake.","solutions":["Bind a concrete non-null key value before executing the statement.","Remove the element-selection term if the key is unknown and filter/read the whole collection instead.","Validate parameters at the application layer so null keys never reach the query."],"exampleFix":"// before\nBoundStatement bs = ps.bind(); // mapKey left unbound/null\nsession.execute(bs.bind(\"SELECT m[?] FROM t WHERE pk = ?\", mapKey, pk));\n// after\nif (mapKey == null) throw new IllegalArgumentException(\"map key required\");\nsession.execute(ps.bind(mapKey, pk));","handlingStrategy":"validation","validationCode":"if (mapKey == null)\n    throw new IllegalArgumentException(\"element selection key must not be null\");\nif (mapKey == ByteBufferUtil.UNSET_BYTE_BUFFER)\n    throw new IllegalArgumentException(\"element selection key must be set\");","typeGuard":null,"tryCatchPattern":"try { session.execute(stmt); }\ncatch (InvalidRequestException e) {\n    if (e.getMessage().contains(\"Invalid null value for element selection\")) { /* fix bound key, retry */ }\n    else throw e;\n}","preventionTips":["Always bind explicit non-null values for element-selection keys.","Reject null/unset map keys in the data-access layer before building statements.","Distinguish 'not provided' (skip the term) from 'null key' in query builders."],"tags":["cassandra","cql","collection","null-value","element-selection"],"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"}