{"record":{"id":"60881642439aa6f1","repo":"apache/beam","slug":"cannot-encode-a-null-integer","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/BigEndianIntegerCoder.java","lineNumber":44,"sourceCode":"\n/** A {@link BigEndianIntegerCoder} encodes {@link Integer Integers} in 4 bytes, big-endian. */\npublic class BigEndianIntegerCoder extends AtomicCoder<Integer> {\n\n  public static BigEndianIntegerCoder of() {\n    return INSTANCE;\n  }\n\n  /////////////////////////////////////////////////////////////////////////////\n\n  private static final BigEndianIntegerCoder INSTANCE = new BigEndianIntegerCoder();\n  private static final TypeDescriptor<Integer> TYPE_DESCRIPTOR = new TypeDescriptor<Integer>() {};\n\n  private BigEndianIntegerCoder() {}\n\n  @Override\n  public void encode(Integer value, OutputStream outStream) throws IOException {\n    if (value == null) {\n      throw new CoderException(\"cannot encode a null Integer\");\n    }\n    BitConverters.writeBigEndianInt(value, outStream);\n  }\n\n  @Override\n  public Integer decode(InputStream inStream) throws IOException, CoderException {\n    try {\n      return BitConverters.readBigEndianInt(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/BigEndianIntegerCoder.java#L26-L62","documentation":"BigEndianIntegerCoder.encode() requires a non-null Integer; Apache Beam coders do not encode null values by default, so encoding null throws CoderException with this message. Nulls must be handled by wrapping the coder (NullableCoder/lengthPrefix) or filtered before encoding.","triggerScenarios":"Passing a null Integer to encode() directly, or a pipeline stage (PCollection<Integer>) whose coder is BigEndianIntegerCoder while the collection can contain null elements.","commonSituations":"Emitting null from a DoFn/map into a PCollection<Integer> without converting to NullableCoder; deserialized data with missing values; Java auto-boxing of an uninitialized int wrapper.","solutions":["Filter nulls before the pipeline stage: p.apply(Filter.by(Objects::nonNull)).","Replace null with a sentinel/default value appropriate to your domain.","Wrap the element type: PCollection<Integer> with coder NullableCoder.of(BigEndianIntegerCoder.of()) via setCoder().","If calling encode() manually, assert value != null first and throw a domain-specific error."],"exampleFix":"// before\nPCollection<Integer> nums = p.apply(\"Parse\", ParDo.of(new ParseIntFn()));\n// after (nulls filtered)\nPCollection<Integer> nums = p.apply(\"Parse\", ParDo.of(new ParseIntFn()))\n    .apply(Filter.by(Objects::nonNull));","handlingStrategy":"validation","validationCode":"if (value == null) { throw new IllegalArgumentException(\"Integer element is null; filter before encoding\"); }","typeGuard":"boolean isEncodable(Integer v) { return v != null; }","tryCatchPattern":"try {\n  coder.encode(value, out);\n} catch (CoderException e) {\n  LOG.error(\"Null Integer in pipeline element\", e);\n  throw e;\n}","preventionTips":["Filter nulls (Filter.by(Objects::nonNull)) at pipeline boundaries","Use NullableCoder when nulls are legitimate values","Avoid returning null from DoFns; prefer Optional handling or sentinels","Set explicit coders on PCollections so null handling is deliberate"],"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"}