apache/beam · error · CoderException
cannot encode a null SortedMap
Error message
cannot encode a null SortedMap
What it means
SortedMapCoder.encode() rejects null SortedMaps, mirroring MapCoder. Beam's wire format for maps has no null representation, so encoding null would corrupt the stream; the coder throws CoderException before writing anything. Use NullableCoder if nulls must be representable.
Solutions
- Replace null with Collections.emptySortedMap() (or new TreeMap<>()) before encoding
- Wrap with NullableCoder.of(sortedMapCoder) if nulls are legitimate values
- Guard the DoFn/transform to skip or default null maps
Example fix
// before out.add(null); // null TreeMap // after out.add(map == null ? Collections.emptySortedMap() : map);
Defensive patterns
Strategy: validation
Validate before calling
if (map == null) { map = Collections.emptySortedMap(); } Type guard
boolean isEncodable(java.util.SortedMap<?,?> m) { return m != null; } Try / catch
try { coder.encode(map, out, Context.OUTER); } catch (CoderException e) { /* handle null map */ } Prevention
- Initialize SortedMap fields to empty maps, not null
- Wrap in NullableCoder when null is valid
- Null-check before emitting in DoFns
When it happens
Trigger: Calling SortedMapCoder.of(kCoder, vCoder).encode(null, out, Context.OUTER), or a pipeline element producing a null TreeMap/SortedMap that gets encoded.
Common situations: DoFn emitting null for a missing sorted map; bean/record fields defaulting to null; deserialized objects with absent maps.
Related errors
- cannot encode a null Byte
- cannot encode a null Double
- cannot encode a null Float
- cannot encode a null Map
- cannot encode a null ReadableDuration
AI-assisted analysis of apache/beam@12126d8942 (2026-09-13).
Data as JSON: /api/errors/633761c50d2dcb27.
Report an issue: GitHub.
Appendix: source
Thrown at sdks/java/core/src/main/java/org/apache/beam/sdk/coders/SortedMapCoder.java:80
private Coder<K> keyCoder;
private Coder<V> valueCoder;
private SortedMapCoder(Coder<K> keyCoder, Coder<V> valueCoder) {
this.keyCoder = keyCoder;
this.valueCoder = valueCoder;
}
@Override
public void encode(SortedMap<K, V> map, OutputStream outStream)
throws IOException, CoderException {
encode(map, outStream, Context.NESTED);
}
@Override
public void encode(SortedMap<K, V> map, OutputStream outStream, Context context)
throws IOException, CoderException {
if (map == null) {
throw new CoderException("cannot encode a null SortedMap");
}
DataOutputStream dataOutStream = new DataOutputStream(outStream);
int size = map.size();
dataOutStream.writeInt(size);
if (size == 0) {
return;
}
// Since we handled size == 0 above, entry is guaranteed to exist before and after loop
Iterator<Entry<K, V>> iterator = map.entrySet().iterator();
Entry<K, V> entry = iterator.next();
while (iterator.hasNext()) {
keyCoder.encode(entry.getKey(), outStream);
valueCoder.encode(entry.getValue(), outStream);
entry = iterator.next();
}
View on GitHub (pinned to 12126d8942)