{"record":{"id":"7ee02af13de06768","repo":"apache/beam","slug":"cannot-encode-a-null-valuekind","errorCode":null,"errorMessage":"cannot encode a null ValueKind","messagePattern":"cannot encode a null ValueKind","errorType":"exception","errorClass":"CoderException","httpStatus":null,"severity":"error","filePath":"sdks/java/core/src/main/java/org/apache/beam/sdk/coders/ValueKindCoder.java","lineNumber":50,"sourceCode":" * Elements.ValueKind.Enum} number, so the wire format stays stable if the enum is reordered and\n * matches the portability representation.\n */\npublic class ValueKindCoder extends AtomicCoder<ValueKind> {\n\n  public static ValueKindCoder of() {\n    return INSTANCE;\n  }\n\n  private static final ValueKindCoder INSTANCE = new ValueKindCoder();\n  private static final TypeDescriptor<ValueKind> TYPE_DESCRIPTOR =\n      TypeDescriptor.of(ValueKind.class);\n\n  private ValueKindCoder() {}\n\n  @Override\n  public void encode(ValueKind value, OutputStream outStream) throws IOException, CoderException {\n    if (value == null) {\n      throw new CoderException(\"cannot encode a null ValueKind\");\n    }\n    outStream.write(ValueKindUtil.toProto(value).getNumber());\n  }\n\n  @Override\n  public ValueKind decode(InputStream inStream) throws IOException, CoderException {\n    int number = inStream.read();\n    if (number == -1) {\n      throw new CoderException(new EOFException(\"EOF encountered decoding a ValueKind\"));\n    }\n    Elements.ValueKind.@Nullable Enum proto = Elements.ValueKind.Enum.forNumber(number);\n    if (proto == null) {\n      throw new CoderException(\"Unknown ValueKind number: \" + number);\n    }\n\n    return ValueKindUtil.fromProto(proto);\n  }\n","sourceCodeStart":32,"sourceCodeEnd":68,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/core/src/main/java/org/apache/beam/sdk/coders/ValueKindCoder.java#L32-L68","documentation":"ValueKindCoder encodes a ValueKind as its protobuf enum number (a single byte) and rejects null with CoderException, since ValueKind has no null member. A null kind indicates an uninitialized field in the surrounding machinery rather than serializable data.","triggerScenarios":"Calling ValueKindCoder.encode(null, out) directly; internal structures where a ValueKind field was never initialized; proto-to-ValueKind conversion code that left the field unset before encoding.","commonSituations":"Runner/harness integration code building element-kind metadata; proto conversions leaving optional ValueKind fields unset; custom pipeline fragment assembly skipping the kind field.","solutions":["Initialize the ValueKind field before encoding (e.g. ValueKind.IMMUTABLE or the appropriate constant).","Default null kinds to a valid sentinel enum constant at construction time.","Verify ValueKindUtil.fromProto returns non-null for every enum number you emit.","Inspect the stack trace to find which builder/constructor left the kind unset."],"exampleFix":"// before\nkindCoder.encode(kind, out); // kind may be null\n// after\nValueKind safeKind = kind != null ? kind : ValueKind.IMMUTABLE;\nkindCoder.encode(safeKind, out);","handlingStrategy":"validation","validationCode":"if (kind == null) {\n  throw new IllegalArgumentException(\"ValueKind must be set before encoding\");\n}","typeGuard":"static boolean encodable(ValueKind k) { return k != null; }","tryCatchPattern":"try {\n  kindCoder.encode(kind, out);\n} catch (CoderException e) {\n  throw new SerializationException(\"Unset ValueKind\", e);\n}","preventionTips":["Initialize all ValueKind fields at object construction.","Default unknown kinds to a valid enum constant.","Ensure ValueKindUtil.fromProto never returns null for emitted numbers."],"tags":["java","beam","coder","null-pointer","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"}