{"record":{"id":"6b07289a0a278dd5","repo":"apache/beam","slug":"cannot-encode-a-null-iterablename","errorCode":null,"errorMessage":"cannot encode a null {iterableName}","messagePattern":"cannot encode a null (.+?)","errorType":"exception","errorClass":"CoderException","httpStatus":null,"severity":"error","filePath":"sdks/java/core/src/main/java/org/apache/beam/sdk/coders/IterableLikeCoder.java","lineNumber":103,"sourceCode":"\n  /////////////////////////////////////////////////////////////////////////////\n  // Internal operations below here.\n\n  private final Coder<T> elementCoder;\n  private final String iterableName;\n\n  protected IterableLikeCoder(Coder<T> elementCoder, String iterableName) {\n    checkArgument(elementCoder != null, \"element Coder for IterableLikeCoder must not be null\");\n    checkArgument(iterableName != null, \"iterable name for IterableLikeCoder must not be null\");\n    this.elementCoder = elementCoder;\n    this.iterableName = iterableName;\n  }\n\n  @Override\n  public void encode(IterableT iterable, OutputStream outStream)\n      throws IOException, CoderException {\n    if (iterable == null) {\n      throw new CoderException(\"cannot encode a null \" + iterableName);\n    }\n    if (iterable instanceof Collection) {\n      // We can know the size of the Iterable.  Use an encoding with a\n      // leading size field, followed by that many elements.\n      Collection<T> collection = (Collection<T>) iterable;\n      BitConverters.writeBigEndianInt(collection.size(), outStream);\n      for (T elem : collection) {\n        elementCoder.encode(elem, outStream);\n      }\n    } else {\n      // We don't know the size without traversing it so use a fixed size buffer\n      // and encode as many elements as possible into it before outputting the size followed\n      // by the elements.\n      BitConverters.writeBigEndianInt(-1, outStream);\n      BufferedElementCountingOutputStream countingOutputStream =\n          new BufferedElementCountingOutputStream(outStream);\n      for (T elem : iterable) {\n        countingOutputStream.markElementStart();","sourceCodeStart":85,"sourceCodeEnd":121,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/core/src/main/java/org/apache/beam/sdk/coders/IterableLikeCoder.java#L85-L121","documentation":"IterableLikeCoder.encode() throws CoderException when the Iterable-like value passed is null. Beam coders have no null representation; the message names the concrete iterable type via iterableName (e.g. \"cannot encode a null List\").","triggerScenarios":"Encoding a null List/Iterable/Set through ListCoder, IterableCoder, SetCoder, etc., either directly or via a pipeline element that is null.","commonSituations":"DoFns that emit KV<K, List<V>> where a grouping produced no values and code passes null instead of an empty collection.","solutions":["Replace null collections with Collections.emptyList() before encoding.","Wrap with NullableCoder.of(...) if nulls must round-trip.","Filter out null collection elements upstream."],"exampleFix":"// before\nc.encode(null, out); // CoderException\n// after\nc.encode(myList == null ? Collections.emptyList() : myList, out);","handlingStrategy":"validation","validationCode":"if (iterable != null) { coder.encode(iterable, out); }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Substitute Collections.emptyList() for null collections before encoding.","Use NullableCoder.of(...) for genuinely optional collections.","Initialize collection fields to empty, never null, in pipeline element classes."],"tags":["java","apache-beam","coders","null","collections"],"backgroundTag":"null-argument","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-14T21:17:11.552Z"}