{"record":{"id":"42302a1472210d00","repo":"apache/cassandra","slug":"invalid-null-value-for-list-index","errorCode":null,"errorMessage":"Invalid null value for list index","messagePattern":"Invalid null value for list index","errorType":"validation","errorClass":"InvalidRequestException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/terms/Lists.java","lineNumber":364,"sourceCode":"        public void collectMarkerSpecification(VariableSpecifications boundNames, Object owner)\n        {\n            super.collectMarkerSpecification(boundNames, owner);\n            idx.collectMarkerSpecification(boundNames, owner);\n        }\n\n        public void execute(DecoratedKey partitionKey, RowUpdateBuilder builder) throws InvalidRequestException\n        {\n            // we should not get here for frozen lists\n            assert column.type.isMultiCell() : \"Attempted to set an individual element on a frozen list\";\n\n            Guardrails.readBeforeWriteListOperationsEnabled\n            .ensureEnabled(\"Setting of list items by index requiring read before write\", builder.clientState);\n\n            ByteBuffer index = idx.bindAndGet(builder);\n            ByteBuffer value = t.bindAndGet(builder);\n\n            if (index == null)\n                throw new InvalidRequestException(\"Invalid null value for list index\");\n            if (index == ByteBufferUtil.UNSET_BYTE_BUFFER)\n                throw new InvalidRequestException(\"Invalid unset value for list index\");\n\n            Row existingRow = builder.getPrefetchedRow(partitionKey, builder.currentClustering());\n            int existingSize = existingSize(existingRow, column);\n            int idx = ByteBufferUtil.toInt(index);\n            if (existingSize == 0)\n                throw new InvalidRequestException(\"Attempted to set an element on a list which is null\");\n            if (idx < 0 || idx >= existingSize)\n                throw new InvalidRequestException(String.format(\"List index %d out of bound, list has size %d\", idx, existingSize));\n\n            CellPath elementPath = existingRow.getComplexColumnData(column).getCellByIndex(idx).path();\n            if (value == null)\n                builder.addTombstone(column, elementPath);\n            else if (value != ByteBufferUtil.UNSET_BYTE_BUFFER)\n                builder.addCell(column, elementPath, value);\n        }\n    }","sourceCodeStart":346,"sourceCodeEnd":382,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/terms/Lists.java#L346-L382","documentation":"InvalidRequestException from the list element setter's execute (Lists.IndexedSetter): a null value was bound for the list index used in an operation like lst[idx] = value on a non-frozen list. The index identifies which element to overwrite, so a null index cannot be resolved; the guard fires during execution after the read-before-write check on multi-cell lists.","triggerScenarios":"`UPDATE t SET l[?] = 'v'` executed with null bound to the index parameter.","commonSituations":"Index computed from user input or a lookup that returned null; DTO field for the index left unset and mapped to null by the driver.","solutions":["Bind a non-null int index","Compute/fail-fast the index in application code before executing","If the element position is unknown, use a different list operation (append/preach whole-list set) instead of indexed set"],"exampleFix":"// before\nps.bind(0, index); // null\nps.bind(1, value);\n// after\nif (index != null) { ps.bind(0, index); ps.bind(1, value); }","handlingStrategy":"validation","validationCode":"if (index == null) throw new IllegalArgumentException(\"list index required\");","typeGuard":null,"tryCatchPattern":"try { session.execute(ps.bind(idx, value)); } catch (InvalidQueryException e) { if (e.getMessage().contains(\"Invalid null value for list index\")) { /* resolve index or skip */ } else throw e; }","preventionTips":["Resolve the index before executing","Fail fast on missing index fields in DTOs","Consider whole-list rewrite when index unknown"],"tags":["cql","list","null-value","bind-parameter"],"backgroundTag":"null-argument","analyzedSha":"88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1","analyzedAt":"2026-09-10T07:29:22.284Z","contentChangedAt":"2026-09-10T07:29:22.284Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}