{"record":{"id":"d198f386f5477ff0","repo":"apache/beam","slug":"cannot-encode-a-null-long","errorCode":null,"errorMessage":"cannot encode a null Long","messagePattern":"cannot encode a null Long","errorType":"exception","errorClass":"CoderException","httpStatus":null,"severity":"error","filePath":"sdks/java/core/src/main/java/org/apache/beam/sdk/coders/BigEndianLongCoder.java","lineNumber":44,"sourceCode":"\n/** A {@link BigEndianLongCoder} encodes {@link Long Longs} in 8 bytes, big-endian. */\npublic class BigEndianLongCoder extends AtomicCoder<Long> {\n\n  public static BigEndianLongCoder of() {\n    return INSTANCE;\n  }\n\n  /////////////////////////////////////////////////////////////////////////////\n\n  private static final BigEndianLongCoder INSTANCE = new BigEndianLongCoder();\n  private static final TypeDescriptor<Long> TYPE_DESCRIPTOR = new TypeDescriptor<Long>() {};\n\n  private BigEndianLongCoder() {}\n\n  @Override\n  public void encode(Long value, OutputStream outStream) throws IOException, CoderException {\n    if (value == null) {\n      throw new CoderException(\"cannot encode a null Long\");\n    }\n    BitConverters.writeBigEndianLong(value, outStream);\n  }\n\n  @Override\n  public Long decode(InputStream inStream) throws IOException, CoderException {\n    try {\n      return 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  @Override\n  public void verifyDeterministic() {}\n","sourceCodeStart":26,"sourceCodeEnd":62,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/core/src/main/java/org/apache/beam/sdk/coders/BigEndianLongCoder.java#L26-L62","documentation":"BigEndianLongCoder.encode() rejects null Long values with a CoderException because Beam coders encode values, not nulls, unless the coder is wrapped in NullableCoder. The message mirrors the Integer coder's null check.","triggerScenarios":"Encoding a null Long directly via BigEndianLongCoder.of().encode(null, out), or a PCollection<Long> containing nulls flowing through a serialization step.","commonSituations":"Map/DoFn outputting null Longs (e.g. failed conversions returning null), aggregations producing null, code migrated from Optional<Long> where null slipped through.","solutions":["Filter nulls: .apply(Filter.by(Objects::nonNull)).","Use NullableCoder.of(BigEndianLongCoder.of()) via setCoder() on the PCollection.","Map null to a domain sentinel (e.g. -1L or 0L) before output.","In direct encode() calls, add a null check before invoking encode."],"exampleFix":"// before\nout.output(value == null ? null : Long.parseLong(value));\n// after\nLong v = value == null ? null : Long.parseLong(value);\nif (v != null) { out.output(v); }","handlingStrategy":"validation","validationCode":"if (value == null) { throw new IllegalArgumentException(\"Long element is null; filter before encoding\"); }","typeGuard":"boolean isEncodable(Long v) { return v != null; }","tryCatchPattern":"try {\n  coder.encode(value, out);\n} catch (CoderException e) {\n  LOG.error(\"Null Long element\", e);\n  throw e;\n}","preventionTips":["Filter nulls upstream of serialization","Use NullableCoder.of(BigEndianLongCoder.of()) for nullable columns","Prefer Optional<Long> or sentinels in DoFn logic over null outputs"],"tags":["java","apache-beam","coder","null","serialization"],"backgroundTag":"null-argument","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}