{"record":{"id":"33c1d6ae24b25e2d","repo":"apache/cassandra","slug":"attempted-to-set-an-element-on-a-list-which-is-nul","errorCode":null,"errorMessage":"Attempted to set an element on a list which is null","messagePattern":"Attempted to set an element on a list which is null","errorType":"validation","errorClass":"InvalidRequestException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/terms/Lists.java","lineNumber":372,"sourceCode":"            // 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    }\n\n    public static class Appender extends Operation\n    {\n        public Appender(ColumnMetadata column, Term t)\n        {\n            super(column, t);\n        }\n","sourceCodeStart":354,"sourceCodeEnd":390,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/terms/Lists.java#L354-L390","documentation":"Thrown when assigning a list element by index but the target list in the prefetched row is empty/null (existingSize == 0). Setting by index requires an existing element to address; there is nothing at any index of a null/empty list.","triggerScenarios":"`UPDATE t SET l[0] = 'v'` executed when row's list l does not exist or is empty; the read-before-write (prefetch) returns no list cells.","commonSituations":"Setting an element on a list column that was never initialized for the row; race where another writer cleared the list between read and write; assuming lists auto-create like collections appends do.","solutions":["Initialize the list first (e.g. `SET l = ['v']`) or append instead of indexing when the list may be empty","Check list contents with a SELECT before indexed writes in application logic","Use appends (l = l + [...]) which work on missing lists"],"exampleFix":"// before\nUPDATE t SET l[0] = 'v' WHERE k = 0; // l missing\n// after\nUPDATE t SET l = l + ['v'] WHERE k = 0; // or ensure l exists first","handlingStrategy":"validation","validationCode":"Row row = session.execute(\"SELECT l FROM t WHERE k=?\", key).one(); if (row == null || row.getList(\"l\", String.class).isEmpty()) throw new IllegalStateException(\"list missing/empty\");","typeGuard":null,"tryCatchPattern":"try { session.execute(stmt); } catch (InvalidQueryException e) { if (e.getMessage().contains(\"list which is null\")) { /* initialize list or append instead */ } else throw e; }","preventionTips":["SELECT the list before indexed writes","Use append for possibly-missing lists","Initialize list columns when creating rows"],"tags":["cql","list","read-before-write","state"],"backgroundTag":"empty-required-field","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"}