{"record":{"id":"77fa2d5a76503e80","repo":"apache/beam","slug":"cannot-encode-a-null-double","errorCode":null,"errorMessage":"cannot encode a null Double","messagePattern":"cannot encode a null Double","errorType":"exception","errorClass":"CoderException","httpStatus":null,"severity":"error","filePath":"sdks/java/core/src/main/java/org/apache/beam/sdk/coders/DoubleCoder.java","lineNumber":45,"sourceCode":"\n/** A {@link DoubleCoder} encodes {@link Double} values in 8 bytes using Java serialization. */\npublic class DoubleCoder extends AtomicCoder<Double> {\n\n  public static DoubleCoder of() {\n    return INSTANCE;\n  }\n\n  /////////////////////////////////////////////////////////////////////////////\n\n  private static final DoubleCoder INSTANCE = new DoubleCoder();\n  private static final TypeDescriptor<Double> TYPE_DESCRIPTOR = new TypeDescriptor<Double>() {};\n\n  private DoubleCoder() {}\n\n  @Override\n  public void encode(Double value, OutputStream outStream) throws IOException, CoderException {\n    if (value == null) {\n      throw new CoderException(\"cannot encode a null Double\");\n    }\n    new DataOutputStream(outStream).writeDouble(value);\n  }\n\n  @Override\n  public Double decode(InputStream inStream) throws IOException, CoderException {\n    try {\n      return Double.longBitsToDouble(BitConverters.readBigEndianLong(inStream));\n    } catch (EOFException | UTFDataFormatException exn) {\n      // These exceptions correspond to decoding problems, so change\n      // what kind of exception they're branded as.\n      throw new CoderException(exn);\n    }\n  }\n\n  /**\n   * {@inheritDoc}\n   *","sourceCodeStart":27,"sourceCodeEnd":63,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/core/src/main/java/org/apache/beam/sdk/coders/DoubleCoder.java#L27-L63","documentation":"DoubleCoder.encode rejects null values by throwing CoderException, because a primitive double encoding has no null representation in Beam's fixed 8-byte format. Beam coders generally require nullable values to be wrapped in a NullableCoder.","triggerScenarios":"Encoding a PCollection<Double> or KV containing a null Double through DoubleCoder, e.g. when a map/DoFn emits null and the coder resolved for the collection is DoubleCoder.","commonSituations":"DoFns computing statistics that emit null for empty input; parsing rows where a field is missing; passing java.util.Optional misuse with null values.","solutions":["Replace nulls before encoding: use a DoFn/Map to substitute a default (e.g. 0.0 or Double.NaN) or filter them out.","Wrap the coder with NullableCoder.of(DoubleCoder.of()) so nulls are encodable.","Change the element type to a nullable-aware representation (e.g. Optional<Double> with an appropriate coder).","Trace the source of the null and fix the producing transform to never emit null."],"exampleFix":"// before\n.apply(MapElements.into(TypeDescriptors.doubles()).via(x -> x.length() > 0 ? parse(x) : null))\n\n// after\n.apply(Filter.by(Objects::nonNull))\n.apply(MapElements.into(TypeDescriptors.doubles()).via(x -> parse(x)))","handlingStrategy":"type-guard","validationCode":"if (value == null) { value = 0.0; /* or filter */ }","typeGuard":"boolean isEncodable(Double d) { return d != null; }","tryCatchPattern":"try {\n  pipeline.run().waitUntilFinish();\n} catch (CoderException | RuntimeException e) {\n  if (e.getMessage() != null && e.getMessage().contains(\"null Double\")) {\n    // add NullableCoder.of(DoubleCoder.of()) or null-filtering and rerun\n  }\n  throw e;\n}","preventionTips":["Never emit nulls from DoFns producing Double; use Optional or sentinel values.","Wrap coders with NullableCoder.of when nulls are legitimate.","Add pipeline-level coder resolution unit tests."],"tags":["beam","java","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-14T16:17:12.679Z"}