apache/beam · error · CoderException

cannot encode a null BitSet

Error message

cannot encode a null BitSet

What it means

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.

Solutions

  1. Filter null BitSets from the PCollection with Filter.by(Objects::nonNull).
  2. Use NullableCoder.of(BitSetCoder.of()) via setCoder() if nulls are valid.
  3. Substitute an empty BitSet (new BitSet()) for null where semantically acceptable.
  4. Null-check before calling encode() in custom code.

Example fix

// before
out.output(bitmap == null ? null : BitSet.valueOf(bitmap));
// after
out.output(bitmap == null ? new BitSet() : BitSet.valueOf(bitmap));
Defensive patterns

Strategy: validation

Validate before calling

if (value == null) { throw new IllegalArgumentException("BitSet element is null; filter before encoding"); }

Type guard

boolean isEncodable(BitSet v) { return v != null; }

Try / catch

try {
  coder.encode(value, out, Context.OUTER);
} catch (CoderException e) {
  LOG.error("Null BitSet element", e);
  throw e;
}

Prevention

When it happens

Trigger: Encoding a null BitSet directly, or a PCollection<BitSet> containing null elements reaching serialization.

Common situations: 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.

Related errors


AI-assisted analysis of apache/beam@12126d8942 (2026-09-13). Data as JSON: /api/errors/22572159458e4a52. Report an issue: GitHub.

Appendix: source

Thrown at sdks/java/core/src/main/java/org/apache/beam/sdk/coders/BitSetCoder.java:45

  private static final BitSetCoder INSTANCE = new BitSetCoder();
  private static final ByteArrayCoder BYTE_ARRAY_CODER = ByteArrayCoder.of();

  private BitSetCoder() {}

  public static BitSetCoder of() {
    return INSTANCE;
  }

  @Override
  public void encode(BitSet value, OutputStream outStream) throws CoderException, IOException {
    encode(value, outStream, Context.NESTED);
  }

  @Override
  public void encode(BitSet value, OutputStream outStream, Context context)
      throws CoderException, IOException {
    if (value == null) {
      throw new CoderException("cannot encode a null BitSet");
    }
    BYTE_ARRAY_CODER.encodeAndOwn(value.toByteArray(), outStream, context);
  }

  @Override
  public BitSet decode(InputStream inStream) throws CoderException, IOException {
    return decode(inStream, Context.NESTED);
  }

  @Override
  public BitSet decode(InputStream inStream, Context context) throws CoderException, IOException {
    return BitSet.valueOf(BYTE_ARRAY_CODER.decode(inStream, context));
  }

  @Override
  public void verifyDeterministic() throws NonDeterministicException {
    verifyDeterministic(
        this, "BitSetCoder requires its ByteArrayCoder to be deterministic.", BYTE_ARRAY_CODER);

View on GitHub (pinned to 12126d8942)