{"record":{"id":"dc0e43070ceeae2f","repo":"apache/beam","slug":"cannot-encode-a-null-integer-varintcoder","errorCode":null,"errorMessage":"cannot encode a null Integer","messagePattern":"cannot encode a null Integer","errorType":"exception","errorClass":"CoderException","httpStatus":null,"severity":"error","filePath":"sdks/java/core/src/main/java/org/apache/beam/sdk/coders/VarIntCoder.java","lineNumber":49,"sourceCode":" * are known to often be large or negative.\n */\npublic class VarIntCoder extends AtomicCoder<Integer> {\n\n  public static VarIntCoder of() {\n    return INSTANCE;\n  }\n\n  /////////////////////////////////////////////////////////////////////////////\n\n  private static final VarIntCoder INSTANCE = new VarIntCoder();\n  private static final TypeDescriptor<Integer> TYPE_DESCRIPTOR = new TypeDescriptor<Integer>() {};\n\n  private VarIntCoder() {}\n\n  @Override\n  public void encode(Integer value, OutputStream outStream) throws IOException, CoderException {\n    if (value == null) {\n      throw new CoderException(\"cannot encode a null Integer\");\n    }\n    VarInt.encode(value, outStream);\n  }\n\n  @Override\n  public Integer decode(InputStream inStream) throws IOException, CoderException {\n    try {\n      return VarInt.decodeInt(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":31,"sourceCodeEnd":67,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/core/src/main/java/org/apache/beam/sdk/coders/VarIntCoder.java#L31-L67","documentation":"VarIntCoder encodes Java Integers as variable-length ints in Beam pipelines. It explicitly rejects null values because the varint wire format has no representation for null; nulls must be handled by wrapping coders (e.g. NullableCoder) before encoding. This fail-fast check prevents corrupted or ambiguous encoded bytes.","triggerScenarios":"Calling VarIntCoder.of().encode(null, outputStream) directly, or a pipeline element whose Integer value is null reaches this coder during encoding (e.g. via a MapElements output or PCollection of Integer containing null without a nullable wrapper).","commonSituations":"Pipeline data contains null Integer values (missing fields in parsed records, absent metrics) flowing through a non-nullable coder; DoFn emits null outputs; user code serializes nulls with raw coders in tests.","solutions":["Ensure the Integer value is non-null before encoding (substitute a default such as 0 or skip the element).","Wrap the coder with NullableCoder.of(VarIntCoder.of()) so nulls are supported.","Filter out or guard null elements upstream in the pipeline before the coder serializes them."],"exampleFix":"// before\ncoder.encode(value, outStream); // NPE-ish CoderException when value == null\n// after\nif (value != null) {\n  coder.encode(value, outStream);\n} else {\n  Coder<Integer> nullable = NullableCoder.of(VarIntCoder.of());\n  nullable.encode(value, outStream);\n}","handlingStrategy":"validation","validationCode":"if (value == null) { value = 0; /* or filter/skip */ }\ncoder.encode(value, outStream);","typeGuard":"boolean isEncodable(Integer v) { return v != null; }","tryCatchPattern":"try { coder.encode(v, out); } catch (CoderException e) { log.error(\"null Integer at encode\", e); }","preventionTips":["Wrap integer coders with NullableCoder.of when nulls are possible","Filter or default null values upstream in DoFns","Run CoderProperties tests with your real data profile"],"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"}