apache/cassandra · error · MarshalException

Invalid null element in set

Error message

Invalid null element in set

What it means

SetType.fromJSONObject() iterates the decoded JSON array and rejects any null element, because Cassandra sets cannot contain null values. A null in the list raises MarshalException before element terms are built.

Source

Thrown at src/java/org/apache/cassandra/db/marshal/SetType.java:240

        return bbs;
    }

    @Override
    public Term fromJSONObject(Object parsed) throws MarshalException
    {
        if (parsed instanceof String)
            parsed = JsonUtils.decodeJson((String) parsed);

        if (!(parsed instanceof List))
            throw new MarshalException(String.format(
                    "Expected a list (representing a set), but got a %s: %s", parsed.getClass().getSimpleName(), parsed));

        List<?> list = (List<?>) parsed;
        List<Term> terms = new ArrayList<>(list.size());
        for (Object element : list)
        {
            if (element == null)
                throw new MarshalException("Invalid null element in set");
            terms.add(elements.fromJSONObject(element));
        }

        return new MultiElements.DelayedValue(this, terms);
    }

    @Override
    public String toJSONString(ByteBuffer buffer, ProtocolVersion protocolVersion)
    {
        return setOrListToJsonString(buffer, elements, protocolVersion);
    }

    @Override
    public void forEach(ByteBuffer input, Consumer<ByteBuffer> action)
    {
        serializer.forEach(input, action);
    }

View on GitHub (pinned to 88fd0f6a0e)

Solutions

  1. Remove null elements from the JSON array before sending.
  2. Filter nulls from the decoded List programmatically before calling fromJSONObject.
  3. Catch MarshalException and surface which set field contained a null element.

Example fix

// before
{"s": [1, null, 3]}
// after
{"s": [1, 3]}
Defensive patterns

Strategy: validation

Validate before calling

List<?> list = (List<?>) decoded;
if (list.stream().anyMatch(Objects::isNull))
    throw new IllegalArgumentException("set contains a null element");

Try / catch

try {
    Term term = setType.fromJSONObject(parsed);
} catch (MarshalException e) {
    if (e.getMessage().contains("null element")) {
        // strip nulls from list and retry
    }
}

Prevention

When it happens

Trigger: Calling fromJSONObject on SetType with a List containing null — e.g. JSON like {"s": [1, null, 3]}.

Common situations: Ingestion pipelines preserving nulls from upstream arrays; clients serializing absent values as null inside JSON arrays; JSON sources containing explicit null array items.

Related errors


AI-assisted analysis of apache/cassandra@88fd0f6a0e (2026-09-10). Data as JSON: /api/errors/f59ab9741afef3c5. Report an issue: GitHub.