{"record":{"id":"50d165921c20ccfd","repo":"apache/beam","slug":"cannot-encode-a-null-float","errorCode":null,"errorMessage":"cannot encode a null Float","messagePattern":"cannot encode a null Float","errorType":"exception","errorClass":"CoderException","httpStatus":null,"severity":"error","filePath":"sdks/java/core/src/main/java/org/apache/beam/sdk/coders/FloatCoder.java","lineNumber":45,"sourceCode":"\n/** A {@link FloatCoder} encodes {@link Float} values in 4 bytes using Java serialization. */\npublic class FloatCoder extends AtomicCoder<Float> {\n\n  public static FloatCoder of() {\n    return INSTANCE;\n  }\n\n  /////////////////////////////////////////////////////////////////////////////\n\n  private static final FloatCoder INSTANCE = new FloatCoder();\n  private static final TypeDescriptor<Float> TYPE_DESCRIPTOR = new TypeDescriptor<Float>() {};\n\n  private FloatCoder() {}\n\n  @Override\n  public void encode(Float value, OutputStream outStream) throws IOException, CoderException {\n    if (value == null) {\n      throw new CoderException(\"cannot encode a null Float\");\n    }\n    new DataOutputStream(outStream).writeFloat(value);\n  }\n\n  @Override\n  public Float decode(InputStream inStream) throws IOException, CoderException {\n    try {\n      return Float.intBitsToFloat(BitConverters.readBigEndianInt(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  /**\n   * {@inheritDoc}\n   *","sourceCodeStart":27,"sourceCodeEnd":63,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/core/src/main/java/org/apache/beam/sdk/coders/FloatCoder.java#L27-L63","documentation":"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.","triggerScenarios":"Encoding a PCollection<Float> (or KV containing null Float) resolved to FloatCoder when a transform emits a null Float element.","commonSituations":"DoFns parsing sensor readings that emit null when data is absent; grouping float values; emitting Optional-style nulls from ML preprocessing.","solutions":["Filter out nulls or substitute defaults (e.g. Float.NaN) before encoding.","Use NullableCoder.of(FloatCoder.of()) if nulls must be preserved.","Fix the producing transform to never emit null floats.","Switch to a boxed-nullable representation with an explicit presence flag if needed."],"exampleFix":"// before\n.apply(MapElements.into(TypeDescriptors.floats()).via(s -> s.isEmpty() ? null : Float.parseFloat(s)))\n\n// after\n.apply(MapElements.into(TypeDescriptors.floats()).via(s -> s.isEmpty() ? Float.NaN : Float.parseFloat(s)))","handlingStrategy":"type-guard","validationCode":"if (value == null) { value = Float.NaN; /* or filter */ }","typeGuard":"boolean isEncodable(Float f) { return f != null; }","tryCatchPattern":"try {\n  pipeline.run().waitUntilFinish();\n} catch (RuntimeException e) {\n  if (e.getMessage() != null && e.getMessage().contains(\"null Float\")) {\n    // add null filtering or NullableCoder.of(FloatCoder.of())\n  }\n  throw e;\n}","preventionTips":["Never emit null Floats from parsing DoFns; use NaN or sentinel defaults.","Wrap coders in NullableCoder.of when nulls must be preserved.","Add coder-resolution tests for all pipeline element types."],"tags":["beam","java","coders","null"],"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"}