{"record":{"id":"18f1e481bd7fce8b","repo":"apache/beam","slug":"cannot-encode-a-null-value-kryocoder","errorCode":null,"errorMessage":"Cannot encode a null value.","messagePattern":"Cannot encode a null value\\.","errorType":"exception","errorClass":"CoderException","httpStatus":null,"severity":"error","filePath":"sdks/java/extensions/kryo/src/main/java/org/apache/beam/sdk/extensions/kryo/KryoCoder.java","lineNumber":196,"sourceCode":"  /** Unique id of the {@link KryoCoder} instance. */\n  private final String instanceId = UUID.randomUUID().toString();\n\n  /** Options for underlying kryo instance. */\n  private final SerializableOptions options;\n\n  /** Client-defined class registrations to {@link Kryo}. */\n  private final List<KryoRegistrar> registrars;\n\n  private KryoCoder(SerializableOptions options, List<KryoRegistrar> registrars) {\n    this.options = options;\n    this.registrars = registrars;\n  }\n\n  @Override\n  public void encode(T value, OutputStream outStream) throws IOException {\n    final KryoState kryoState = KryoState.get(this);\n    if (value == null) {\n      throw new CoderException(\"Cannot encode a null value.\");\n    }\n    final OutputChunked outputChunked = kryoState.getOutputChunked();\n    outputChunked.setOutputStream(outStream);\n    try {\n      kryoState.getKryo().writeClassAndObject(outputChunked, value);\n      outputChunked.endChunk();\n      outputChunked.flush();\n    } catch (KryoException e) {\n      outputChunked.reset();\n      if (e.getCause() instanceof EOFException) {\n        throw (EOFException) e.getCause();\n      }\n      throw new CoderException(\"Cannot encode given object of type [\" + value.getClass() + \"].\", e);\n    } catch (IllegalArgumentException e) {\n      String message = e.getMessage();\n      if (message != null) {\n        if (message.startsWith(\"Class is not registered\")) {\n          throw new CoderException(message);","sourceCodeStart":178,"sourceCodeEnd":214,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/extensions/kryo/src/main/java/org/apache/beam/sdk/extensions/kryo/KryoCoder.java#L178-L214","documentation":"KryoCoder.encode() does not support null values; it deliberately throws CoderException(\"Cannot encode a null value.\") before invoking Kryo serialization. Beam coders conventionally reject nulls — nulls must be handled at the PCollection/schema level, not inside the coder.","triggerScenarios":"Encoding a PCollection element that is null at runtime — e.g. a KV/DoFn emitted null, a parse produced null, or a nullable field feeding a Kryo-coded PCollection.","commonSituations":"Map/DoFn returning null for filtered records instead of dropping them; JSON/CSV parsing yielding null rows; joins producing null values that flow into the Kryo-coded collection.","solutions":["Filter out null elements before they reach the coder: pipeline.apply(Filter.by(e -> e != null))","Emit a sentinel/wrapper object (e.g. Optional or a Present/Absent type) instead of null","Fix the producing DoFn to skip null results rather than returning them","If nulls are legitimate, wrap in a Nullable-aware container type that Kryo can encode"],"exampleFix":"// before\nreturn parse(record); // may return null\n// after\nMyValue v = parse(record);\nreturn v == null ? null : v; // use Filter.by(v -> v != null) downstream, or return Optional-wrapped value","handlingStrategy":"type-guard","validationCode":"java\npcollection.apply(Filter.by(v -> v != null));","typeGuard":"java\nstatic <T> boolean isPresent(T v) { return v != null; }","tryCatchPattern":"java\ntry {\n  coder.encode(value, out);\n} catch (CoderException e) {\n  if (\"Cannot encode a null value.\".equals(e.getMessage())) {\n    // nulls are unsupported: fix upstream DoFn/filter instead of retrying\n  }\n}","preventionTips":["Never emit null from DoFns; drop or wrap instead","Add Filter.by(Objects::nonNull) before Kryo-coded stages","Use Optional<T> for optional values","Document coder nullability contract for team pipelines"],"tags":["java","beam","kryo","serialization","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"}