gradle/gradle · error · IllegalArgumentException
Cannot encode a null string.
Error message
Cannot encode a null string.
What it means
This encoder deduplicates repeated strings and encodes null via a special NULL_STRING index — but only through writeNullableString. Its writeString still rejects null, because the non-null path writes a dedup index with no null representation.
Source
Thrown at platforms/core-runtime/serialization/src/main/java/org/gradle/internal/serialize/kryo/StringDeduplicatingKryoBackedEncoder.java:110
@Override
public void writeBoolean(boolean value) {
output.writeBoolean(value);
}
@Override
public void writeNullableString(@Nullable CharSequence value) {
if (value == null) {
writeStringIndex(NULL_STRING);
return;
}
writeNonnullString(value);
}
@Override
public void writeString(CharSequence value) {
if (value == null) {
throw new IllegalArgumentException("Cannot encode a null string.");
}
writeNonnullString(value);
}
private void writeNonnullString(CharSequence value) {
String key = value.toString();
if (strings == null) {
strings = new Object2IntOpenHashMap<>(1024);
} else {
int index = strings.getOrDefault(key, -1);
if (index != -1) {
writeStringIndex(index);
return;
}
}
/*
Actual stored string indices start from 2 so `0` and `1` can be used as special codes:View on GitHub (pinned to 534f27719b)
Solutions
- Route optional strings through writeNullableString(value), which emits the NULL_STRING index.
- Or guard and substitute "" when empty is an acceptable value.
- Remove nulls upstream so the non-null path is always valid.
Example fix
// before encoder.writeString(pathOrNull); // throws "Cannot encode a null string." // after encoder.writeNullableString(pathOrNull);
Defensive patterns
Strategy: validation
Validate before calling
if (value == null) {
encoder.writeNullableString(null); // emits NULL_STRING index
} else {
encoder.writeString(value);
} Prevention
- Only writeNullableString may encode null on the deduplicating encoder.
- When adding optional fields to a deduplicated codec, update writer and reader together.
- Re-run dedup cache tests with null values before shipping.
When it happens
Trigger: Calling writeString(null) on the string-deduplicating encoder; forwarding an optional CharSequence field to writeString instead of writeNullableString before dedup lookup.
Common situations: Long-lived caches that use string deduplication where optional fields were later introduced; the same class of null-handling mistake as the other encoders, discovered only when the field is actually null at runtime.
Related errors
- Cannot encode a null string.
- Cannot encode a null string.
- Expecting the end of nested stream.
- Could not write cache value to '%s'.
- Could not read cache value from '%s'.
AI-assisted analysis of gradle/gradle@534f27719b (2026-08-22).
Data as JSON: /api/errors/7cd6295a2278f0c9.
Report an issue: GitHub.