{"record":{"id":"aa7203da0d2b096d","repo":"apache/beam","slug":"cannot-encode-a-null-messageclassname","errorCode":null,"errorMessage":"cannot encode a null ${messageClassName}","messagePattern":"cannot encode a null (.+?)","errorType":"exception","errorClass":"org.apache.beam.sdk.coders.CoderException","httpStatus":null,"severity":"error","filePath":"sdks/java/extensions/protobuf/src/main/java/org/apache/beam/sdk/extensions/protobuf/ProtoCoder.java","lineNumber":188,"sourceCode":"\n  /**\n   * See {@link #withExtensionsFrom(Iterable)}.\n   *\n   * <p>Does not modify this object.\n   */\n  public ProtoCoder<T> withExtensionsFrom(Class<?>... moreExtensionHosts) {\n    return withExtensionsFrom(Arrays.asList(moreExtensionHosts));\n  }\n\n  @Override\n  public void encode(T value, OutputStream outStream) throws IOException {\n    encode(value, outStream, Context.NESTED);\n  }\n\n  @Override\n  public void encode(T value, OutputStream outStream, Context context) throws IOException {\n    if (value == null) {\n      throw new CoderException(\"cannot encode a null \" + protoMessageClass.getSimpleName());\n    }\n    if (context.isWholeStream) {\n      value.writeTo(outStream);\n    } else {\n      value.writeDelimitedTo(outStream);\n    }\n  }\n\n  @Override\n  public T decode(InputStream inStream) throws IOException {\n    return decode(inStream, Context.NESTED);\n  }\n\n  @Override\n  public T decode(InputStream inStream, Context context) throws IOException {\n    if (context.isWholeStream) {\n      return getParser().parseFrom(inStream, getExtensionRegistry());\n    } else {","sourceCodeStart":170,"sourceCodeEnd":206,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/extensions/protobuf/src/main/java/org/apache/beam/sdk/extensions/protobuf/ProtoCoder.java#L170-L206","documentation":"ProtoCoder does not accept null values during encoding; a null proto message cannot be serialized to the output stream. This CoderException signals a contract violation by the pipeline element that tried to encode null.","triggerScenarios":"A PCollection containing null elements is encoded with ProtoCoder, e.g. when a DoFn/ParDo emits null or a downstream sink encodes null messages, and encode(T, OutputStream, Context) is invoked with value == null.","commonSituations":"Map/filter chains producing null outputs; aggregations over missing keys yielding null; accidental null returns in user code feeding a coded sink or shuffle.","solutions":["Filter nulls before the coder is used: pcollection.apply(Filter.by(m -> m != null)).","Replace nulls with a default instance (e.g. Message.getDefaultInstance()).","Fix the upstream transform to never emit null.","Use a nullable representation (Optional/PCollectionList) instead of encoding nulls."],"exampleFix":"// before\nreturn maybeMessage; // may be null\n// after\nreturn maybeMessage != null ? maybeMessage : MyProto.getDefaultInstance();","handlingStrategy":"validation","validationCode":"if (msg == null) msg = MyProto.getDefaultInstance(); // before handing to coder","typeGuard":null,"tryCatchPattern":"try { coder.encode(msg, out, Context.OUTER); } catch (CoderException e) { log.error(\"null or malformed message\", e); }","preventionTips":["Filter null elements before sinks/shuffles.","Default to Message.getDefaultInstance() for absent values.","Avoid transforms that can emit null into coded PCollections."],"tags":["java","protobuf","null-pointer"],"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"}