{"record":{"id":"0deb9a5cfc0c2550","repo":"apache/beam","slug":"cannot-encode-a-null-long-varlongcoder","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/VarLongCoder.java","lineNumber":50,"sourceCode":" * always take 10 bytes, so {@link BigEndianLongCoder} may be preferable for longs that are known to\n * often be large or negative.\n */\npublic class VarLongCoder extends StructuredCoder<Long> {\n  public static VarLongCoder of() {\n    return INSTANCE;\n  }\n\n  /////////////////////////////////////////////////////////////////////////////\n\n  private static final VarLongCoder INSTANCE = new VarLongCoder();\n  private static final TypeDescriptor<Long> TYPE_DESCRIPTOR = new TypeDescriptor<Long>() {};\n\n  private VarLongCoder() {}\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    VarInt.encode(value, outStream);\n  }\n\n  @Override\n  public Long decode(InputStream inStream) throws IOException, CoderException {\n    try {\n      return VarInt.decodeLong(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 List<? extends Coder<?>> getCoderArguments() {\n    return Collections.emptyList();","sourceCodeStart":32,"sourceCodeEnd":68,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/core/src/main/java/org/apache/beam/sdk/coders/VarLongCoder.java#L32-L68","documentation":"VarLongCoder encodes Java Longs as variable-length ints and, like VarIntCoder, rejects nulls at encode time because the varint format cannot represent null. Throwing CoderException here surfaces the problem at the point of serialization instead of producing corrupt bytes.","triggerScenarios":"Calling VarLongCoder.of().encode(null, outStream), or a PCollection<Long>/state value/timer timestamp field being null when this coder serializes it.","commonSituations":"Null Long values from missing data fields (e.g. absent counts, unmapped joins), DoFn emitting null Longs, user code buffering null values into Beam state or side inputs.","solutions":["Replace null Longs with defaults (e.g. 0L) or filter them before encoding.","Wrap the coder: NullableCoder.of(VarLongCoder.of()).","Check upstream parsing/mapping logic that converts strings/records to Long and produces nulls."],"exampleFix":"// before\nVarLongCoder.of().encode(maybeNullLong, outStream);\n// after\nCoder<Long> coder = NullableCoder.of(VarLongCoder.of());\ncoder.encode(maybeNullLong, outStream); // handles null","handlingStrategy":"validation","validationCode":"if (value == null) { value = 0L; }\ncoder.encode(value, outStream);","typeGuard":"boolean isEncodable(Long v) { return v != null; }","tryCatchPattern":"try { coder.encode(v, out); } catch (CoderException e) { throw new IOException(\"null Long at encode\", e); }","preventionTips":["Default null Longs at parse time (OptionalLong or 0L)","Use NullableCoder.of(VarLongCoder.of()) for nullable fields","Audit mapping code that converts records to Long for null outputs"],"tags":["beam","coder","null-value","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"}