{"record":{"id":"22572159458e4a52","repo":"apache/beam","slug":"cannot-encode-a-null-bitset","errorCode":null,"errorMessage":"cannot encode a null BitSet","messagePattern":"cannot encode a null BitSet","errorType":"exception","errorClass":"CoderException","httpStatus":null,"severity":"error","filePath":"sdks/java/core/src/main/java/org/apache/beam/sdk/coders/BitSetCoder.java","lineNumber":45,"sourceCode":"  private static final BitSetCoder INSTANCE = new BitSetCoder();\n  private static final ByteArrayCoder BYTE_ARRAY_CODER = ByteArrayCoder.of();\n\n  private BitSetCoder() {}\n\n  public static BitSetCoder of() {\n    return INSTANCE;\n  }\n\n  @Override\n  public void encode(BitSet value, OutputStream outStream) throws CoderException, IOException {\n    encode(value, outStream, Context.NESTED);\n  }\n\n  @Override\n  public void encode(BitSet value, OutputStream outStream, Context context)\n      throws CoderException, IOException {\n    if (value == null) {\n      throw new CoderException(\"cannot encode a null BitSet\");\n    }\n    BYTE_ARRAY_CODER.encodeAndOwn(value.toByteArray(), outStream, context);\n  }\n\n  @Override\n  public BitSet decode(InputStream inStream) throws CoderException, IOException {\n    return decode(inStream, Context.NESTED);\n  }\n\n  @Override\n  public BitSet decode(InputStream inStream, Context context) throws CoderException, IOException {\n    return BitSet.valueOf(BYTE_ARRAY_CODER.decode(inStream, context));\n  }\n\n  @Override\n  public void verifyDeterministic() throws NonDeterministicException {\n    verifyDeterministic(\n        this, \"BitSetCoder requires its ByteArrayCoder to be deterministic.\", BYTE_ARRAY_CODER);","sourceCodeStart":27,"sourceCodeEnd":63,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/core/src/main/java/org/apache/beam/sdk/coders/BitSetCoder.java#L27-L63","documentation":"BitSetCoder.encode was handed a null BitSet. The coder serializes the BitSet's byte-length plus bytes and cannot represent absence; per Coder contract, null values must be handled by an outer NullableCoder. The null value argument is at fault, not the coder configuration.","triggerScenarios":"Encoding a null BitSet directly, or a PCollection<BitSet> containing null elements reaching serialization.","commonSituations":"Null BitSets returned from helper methods (e.g. building a bitset from a bitmap that was absent), aggregation results that are null, manual coder use in tests.","solutions":["Filter null BitSets from the PCollection with Filter.by(Objects::nonNull).","Use NullableCoder.of(BitSetCoder.of()) via setCoder() if nulls are valid.","Substitute an empty BitSet (new BitSet()) for null where semantically acceptable.","Null-check before calling encode() in custom code."],"exampleFix":"// before\nout.output(bitmap == null ? null : BitSet.valueOf(bitmap));\n// after\nout.output(bitmap == null ? new BitSet() : BitSet.valueOf(bitmap));","handlingStrategy":"validation","validationCode":"if (value == null) { throw new IllegalArgumentException(\"BitSet element is null; filter before encoding\"); }","typeGuard":"boolean isEncodable(BitSet v) { return v != null; }","tryCatchPattern":"try {\n  coder.encode(value, out, Context.OUTER);\n} catch (CoderException e) {\n  LOG.error(\"Null BitSet element\", e);\n  throw e;\n}","preventionTips":["Return new BitSet() instead of null from bitset builders","Filter null BitSets at PCollection boundaries","Wrap with NullableCoder.of(BitSetCoder.of()) when nulls are meaningful"],"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"}