{"record":{"id":"840595d86d0c4d20","repo":"apache/beam","slug":"cannot-encode-a-null-short","errorCode":null,"errorMessage":"cannot encode a null Short","messagePattern":"cannot encode a null Short","errorType":"exception","errorClass":"CoderException","httpStatus":null,"severity":"error","filePath":"sdks/java/core/src/main/java/org/apache/beam/sdk/coders/BigEndianShortCoder.java","lineNumber":44,"sourceCode":"\n/** A {@link BigEndianShortCoder} encodes {@link Short Shorts} in 2 bytes, big-endian. */\npublic class BigEndianShortCoder extends AtomicCoder<Short> {\n\n  public static BigEndianShortCoder of() {\n    return INSTANCE;\n  }\n\n  /////////////////////////////////////////////////////////////////////////////\n\n  private static final BigEndianShortCoder INSTANCE = new BigEndianShortCoder();\n  private static final TypeDescriptor<Short> TYPE_DESCRIPTOR = new TypeDescriptor<Short>() {};\n\n  private BigEndianShortCoder() {}\n\n  @Override\n  public void encode(Short value, OutputStream outStream) throws IOException {\n    if (value == null) {\n      throw new CoderException(\"cannot encode a null Short\");\n    }\n    BitConverters.writeBigEndianShort(value, outStream);\n  }\n\n  @Override\n  public Short decode(InputStream inStream) throws IOException, CoderException {\n    try {\n      return BitConverters.readBigEndianShort(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/BigEndianShortCoder.java#L26-L62","documentation":"BigEndianShortCoder.encode refuses a null Short: AtomicCoder implementations encode a fixed 2-byte big-endian representation and have no null marker, so a null value passed to encode cannot be serialized. The null Short argument is the input at fault.","triggerScenarios":"Calling BigEndianShortCoder.of().encode(null, out) directly, or a PCollection<Short> containing null elements reaching an encode step.","commonSituations":"Null Shorts from boxed conversions (e.g. Number.shortValue() on null), sparse data mapped to Short, hand-rolled DoFns emitting null on parse failure.","solutions":["Filter nulls with Filter.by(Objects::nonNull) before the stage.","Use NullableCoder.of(BigEndianShortCoder.of()) via setCoder().","Coerce nulls to a default short value upstream.","Add an explicit null check in custom code before calling encode()."],"exampleFix":"// before\nout.output(raw == null ? null : Short.parseShort(raw));\n// after\nShort s = raw == null ? null : Short.parseShort(raw);\nif (s != null) { out.output(s); }","handlingStrategy":"validation","validationCode":"if (value == null) { throw new IllegalArgumentException(\"Short element is null; filter before encoding\"); }","typeGuard":"boolean isEncodable(Short v) { return v != null; }","tryCatchPattern":"try {\n  coder.encode(value, out);\n} catch (CoderException e) {\n  LOG.error(\"Null Short element\", e);\n  throw e;\n}","preventionTips":["Guard boxed conversions that can yield null Shorts","Filter nulls at pipeline inputs","Use NullableCoder for nullable Short collections"],"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-14T16:17:12.679Z"}