{"record":{"id":"5649b908116e5ff1","repo":"apache/beam","slug":"error-when-decoding-a-textual-integer","errorCode":null,"errorMessage":"error when decoding a textual integer","messagePattern":"error when decoding a textual integer","errorType":"exception","errorClass":"CoderException","httpStatus":null,"severity":"error","filePath":"sdks/java/core/src/main/java/org/apache/beam/sdk/coders/TextualIntegerCoder.java","lineNumber":67,"sourceCode":"    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    }\n  }\n\n  @Override\n  public void verifyDeterministic() {\n    StringUtf8Coder.of().verifyDeterministic();\n  }\n\n  @Override\n  public TypeDescriptor<Integer> getEncodedTypeDescriptor() {\n    return TYPE_DESCRIPTOR;\n  }\n\n  @Override\n  protected long getEncodedElementByteSize(Integer value) throws Exception {\n    if (value == null) {\n      throw new CoderException(\"cannot encode a null Integer\");\n    }","sourceCodeStart":49,"sourceCodeEnd":85,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/core/src/main/java/org/apache/beam/sdk/coders/TextualIntegerCoder.java#L49-L85","documentation":"TextualIntegerCoder.decode first decodes a UTF-8 string, then parses it with Integer.valueOf. If the string is not a valid signed 32-bit decimal integer (empty, non-numeric, or beyond Integer range), the NumberFormatException is caught and rethrown as CoderException with this message and the original as cause.","triggerScenarios":"Decoding bytes written by a different coder (e.g. binary VarIntCoder instead of textual); corrupted or truncated stream producing garbage text; values exceeding Integer.MAX_VALUE/MIN_VALUE; hand-edited text data read back with this coder.","commonSituations":"Switching an element's coder between VarIntCoder and TextualIntegerCoder so old data no longer decodes; reading external text with non-numeric values into an Integer-coded PCollection; formatted data with separators like commas.","solutions":["Ensure the stream was written by TextualIntegerCoder.encode (decimal string with StringUtf8 length prefix).","Log the failing string from the exception cause to identify the bad data.","Use VarIntCoder or BigEndianIntegerCoder to match the original binary format if data was not textual.","Switch to a Long coder (e.g. TextualLongCoder) if values may exceed int range."],"exampleFix":"// before\nInteger v = TextualIntegerCoder.of().decode(in);\n// after\ntry {\n  Integer v = TextualIntegerCoder.of().decode(in);\n} catch (CoderException e) {\n  throw new IllegalStateException(\"Non-numeric or out-of-range data in stream\", e);\n}","handlingStrategy":"try-catch","validationCode":"String s = decodeStringSomehow(in);\nif (s == null || !s.matches(\"-?\\\\d+\")) {\n  throw new IllegalArgumentException(\"Not a textual integer: \" + s);\n}","typeGuard":"static boolean isParsableInt(String s) {\n  try { Integer.parseInt(s); return true; } catch (NumberFormatException e) { return false; }\n}","tryCatchPattern":"try {\n  Integer v = TextualIntegerCoder.of().decode(in);\n} catch (CoderException e) {\n  Throwable cause = e.getCause();\n  if (cause instanceof NumberFormatException) {\n    throw new DataCorruptionException(\"Non-numeric data in stream: \" + cause.getMessage(), e);\n  }\n  throw e;\n}","preventionTips":["Never change an element's coder on data already persisted in the old format.","Log the offending string from the exception cause when diagnosing.","Validate external text data before loading it into Integer-coded PCollections.","Use a Long coder when values may exceed Integer range."],"tags":["java","beam","coder","numberformat","deserialization"],"backgroundTag":"invalid-argument-format","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"}