apache/beam · error · CoderException

cannot encode a null Float

Error message

cannot encode a null Float

What it means

FloatCoder.encode rejects null values with CoderException because the 4-byte IEEE-754 float encoding has no null slot. As with DoubleCoder, nullable floats require a NullableCoder wrapper or must be eliminated upstream.

Source

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

/** A {@link FloatCoder} encodes {@link Float} values in 4 bytes using Java serialization. */
public class FloatCoder extends AtomicCoder<Float> {

  public static FloatCoder of() {
    return INSTANCE;
  }

  /////////////////////////////////////////////////////////////////////////////

  private static final FloatCoder INSTANCE = new FloatCoder();
  private static final TypeDescriptor<Float> TYPE_DESCRIPTOR = new TypeDescriptor<Float>() {};

  private FloatCoder() {}

  @Override
  public void encode(Float value, OutputStream outStream) throws IOException, CoderException {
    if (value == null) {
      throw new CoderException("cannot encode a null Float");
    }
    new DataOutputStream(outStream).writeFloat(value);
  }

  @Override
  public Float decode(InputStream inStream) throws IOException, CoderException {
    try {
      return Float.intBitsToFloat(BitConverters.readBigEndianInt(inStream));
    } catch (EOFException | UTFDataFormatException exn) {
      // These exceptions correspond to decoding problems, so change
      // what kind of exception they're branded as.
      throw new CoderException(exn);
    }
  }

  /**
   * {@inheritDoc}
   *

View on GitHub (pinned to 12126d8942)

Solutions

  1. Filter out nulls or substitute defaults (e.g. Float.NaN) before encoding.
  2. Use NullableCoder.of(FloatCoder.of()) if nulls must be preserved.
  3. Fix the producing transform to never emit null floats.
  4. Switch to a boxed-nullable representation with an explicit presence flag if needed.

Example fix

// before
.apply(MapElements.into(TypeDescriptors.floats()).via(s -> s.isEmpty() ? null : Float.parseFloat(s)))

// after
.apply(MapElements.into(TypeDescriptors.floats()).via(s -> s.isEmpty() ? Float.NaN : Float.parseFloat(s)))
Defensive patterns

Strategy: type-guard

Validate before calling

if (value == null) { value = Float.NaN; /* or filter */ }

Type guard

boolean isEncodable(Float f) { return f != null; }

Try / catch

try {
  pipeline.run().waitUntilFinish();
} catch (RuntimeException e) {
  if (e.getMessage() != null && e.getMessage().contains("null Float")) {
    // add null filtering or NullableCoder.of(FloatCoder.of())
  }
  throw e;
}

Prevention

When it happens

Trigger: Encoding a PCollection<Float> (or KV containing null Float) resolved to FloatCoder when a transform emits a null Float element.

Common situations: DoFns parsing sensor readings that emit null when data is absent; grouping float values; emitting Optional-style nulls from ML preprocessing.

Related errors


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