{"record":{"id":"348e5cd99ad55d28","repo":"apache/beam","slug":"cannot-encode-a-null-integer-textualintegercoder","errorCode":null,"errorMessage":"cannot encode a null Integer","messagePattern":"cannot encode a null Integer","errorType":"exception","errorClass":"CoderException","httpStatus":null,"severity":"error","filePath":"sdks/java/core/src/main/java/org/apache/beam/sdk/coders/TextualIntegerCoder.java","lineNumber":50,"sourceCode":"    return new TextualIntegerCoder();\n  }\n\n  /////////////////////////////////////////////////////////////////////////////\n\n  private static final TypeDescriptor<Integer> TYPE_DESCRIPTOR = new TypeDescriptor<Integer>() {};\n\n  protected TextualIntegerCoder() {}\n\n  @Override\n  public void encode(Integer value, OutputStream outStream) throws IOException, CoderException {\n    encode(value, outStream, Context.NESTED);\n  }\n\n  @Override\n  public void encode(Integer value, OutputStream outStream, Context context)\n      throws IOException, CoderException {\n    if (value == null) {\n      throw new CoderException(\"cannot encode a null Integer\");\n    }\n    String textualValue = value.toString();\n    StringUtf8Coder.of().encode(textualValue, outStream, context);\n  }\n\n  @Override\n  public Integer decode(InputStream inStream) throws IOException, CoderException {\n    return decode(inStream, Context.NESTED);\n  }\n\n  @Override\n  public Integer decode(InputStream inStream, Context context) throws IOException, CoderException {\n    String textualValue = StringUtf8Coder.of().decode(inStream, context);\n    try {\n      return Integer.valueOf(textualValue);\n    } catch (NumberFormatException exn) {\n      throw new CoderException(\"error when decoding a textual integer\", exn);\n    }","sourceCodeStart":32,"sourceCodeEnd":68,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/core/src/main/java/org/apache/beam/sdk/coders/TextualIntegerCoder.java#L32-L68","documentation":"TextualIntegerCoder encodes Integers as their decimal string form (delegating to StringUtf8Coder) and does not support null, since there is no textual form for null. It throws CoderException; wrap with NullableCoder if nulls must round-trip.","triggerScenarios":"Calling TextualIntegerCoder.of().encode(null, out, context) directly; a PCollection<Integer> containing null values encoded with TextualIntegerCoder (counts or lookups that produced null); text sinks fed null integers.","commonSituations":"Side inputs or lookup joins returning null for missing keys; parse transformations yielding null Integer that are then written through the coder; refactors turning boxed defaults into nulls.","solutions":["Coalesce nulls to a default (e.g. 0 or -1) before encoding.","Wrap with NullableCoder.of(TextualIntegerCoder.of()) and set it as the PCollection coder.","Filter null elements before the sink/encode step.","Fix the upstream transform to produce a non-null value or handle Optional explicitly."],"exampleFix":"// before\nTextualIntegerCoder.of().encode(value, out, Context.OUTER);\n// after\nint safe = value == null ? 0 : value;\nTextualIntegerCoder.of().encode(safe, out, Context.OUTER);","handlingStrategy":"validation","validationCode":"if (value == null) {\n  throw new IllegalArgumentException(\"Cannot encode null Integer with TextualIntegerCoder; provide a default or use NullableCoder\");\n}","typeGuard":"static boolean encodable(Integer v) { return v != null; }","tryCatchPattern":"try {\n  TextualIntegerCoder.of().encode(value, out, ctx);\n} catch (CoderException e) {\n  throw new SerializationException(\"Null or unencodable Integer\", e);\n}","preventionTips":["Coalesce null integers to defaults before coding.","Prefer NullableCoder.of(...) when nulls are legal data.","Ensure joins/lookups produce non-null results or explicit Optional handling.","Test pipelines with sources that can emit null values."],"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"}