{"record":{"id":"633761c50d2dcb27","repo":"apache/beam","slug":"cannot-encode-a-null-sortedmap","errorCode":null,"errorMessage":"cannot encode a null SortedMap","messagePattern":"cannot encode a null SortedMap","errorType":"exception","errorClass":"CoderException","httpStatus":null,"severity":"error","filePath":"sdks/java/core/src/main/java/org/apache/beam/sdk/coders/SortedMapCoder.java","lineNumber":80,"sourceCode":"  private Coder<K> keyCoder;\n  private Coder<V> valueCoder;\n\n  private SortedMapCoder(Coder<K> keyCoder, Coder<V> valueCoder) {\n    this.keyCoder = keyCoder;\n    this.valueCoder = valueCoder;\n  }\n\n  @Override\n  public void encode(SortedMap<K, V> map, OutputStream outStream)\n      throws IOException, CoderException {\n    encode(map, outStream, Context.NESTED);\n  }\n\n  @Override\n  public void encode(SortedMap<K, V> map, OutputStream outStream, Context context)\n      throws IOException, CoderException {\n    if (map == null) {\n      throw new CoderException(\"cannot encode a null SortedMap\");\n    }\n    DataOutputStream dataOutStream = new DataOutputStream(outStream);\n\n    int size = map.size();\n    dataOutStream.writeInt(size);\n    if (size == 0) {\n      return;\n    }\n\n    // Since we handled size == 0 above, entry is guaranteed to exist before and after loop\n    Iterator<Entry<K, V>> iterator = map.entrySet().iterator();\n    Entry<K, V> entry = iterator.next();\n    while (iterator.hasNext()) {\n      keyCoder.encode(entry.getKey(), outStream);\n      valueCoder.encode(entry.getValue(), outStream);\n      entry = iterator.next();\n    }\n","sourceCodeStart":62,"sourceCodeEnd":98,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/core/src/main/java/org/apache/beam/sdk/coders/SortedMapCoder.java#L62-L98","documentation":"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.","triggerScenarios":"Calling SortedMapCoder.of(kCoder, vCoder).encode(null, out, Context.OUTER), or a pipeline element producing a null TreeMap/SortedMap that gets encoded.","commonSituations":"DoFn emitting null for a missing sorted map; bean/record fields defaulting to null; deserialized objects with absent maps.","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"],"exampleFix":"// before\nout.add(null); // null TreeMap\n// after\nout.add(map == null ? Collections.emptySortedMap() : map);","handlingStrategy":"validation","validationCode":"if (map == null) { map = Collections.emptySortedMap(); }","typeGuard":"boolean isEncodable(java.util.SortedMap<?,?> m) { return m != null; }","tryCatchPattern":"try { coder.encode(map, out, Context.OUTER); } catch (CoderException e) { /* handle null map */ }","preventionTips":["Initialize SortedMap fields to empty maps, not null","Wrap in NullableCoder when null is valid","Null-check before emitting in DoFns"],"tags":["java","beam","coders","null"],"backgroundTag":"null-argument","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-20T03:17:13.778Z"}