apache/flink · error · NumberFormatException

There is leading or trailing whitespace in the numeric field

Error message

There is leading or trailing whitespace in the numeric field.

What it means

Thrown by FloatParser.parseField when a float text field has leading or trailing whitespace. After limiting the field to the delimiter via nextStringLength, the parser checks the boundary bytes with Character.isWhitespace and rejects before calling Float.parseFloat.

Source

Thrown at flink-core/src/main/java/org/apache/flink/types/parser/FloatParser.java:98

    /**
     * Static utility to parse a field of type float from a byte sequence that represents text
     * characters (such as when read from a file stream).
     *
     * @param bytes The bytes containing the text data that should be parsed.
     * @param startPos The offset to start the parsing.
     * @param length The length of the byte sequence (counting from the offset).
     * @param delimiter The delimiter that terminates the field.
     * @return The parsed value.
     * @throws IllegalArgumentException Thrown when the value cannot be parsed because the text
     *     represents not a correct number.
     */
    public static final float parseField(byte[] bytes, int startPos, int length, char delimiter) {
        final int limitedLen = nextStringLength(bytes, startPos, length, delimiter);

        if (limitedLen > 0
                && (Character.isWhitespace(bytes[startPos])
                        || Character.isWhitespace(bytes[startPos + limitedLen - 1]))) {
            throw new NumberFormatException(
                    "There is leading or trailing whitespace in the numeric field.");
        }

        final String str = new String(bytes, startPos, limitedLen, ConfigConstants.DEFAULT_CHARSET);
        return Float.parseFloat(str);
    }
}

View on GitHub (pinned to 2f3c205e92)

Solutions

  1. Trim the field bytes before parsing.
  2. Normalize CRLF input and configure delimiters so padding stays outside numeric columns.
  3. Validate representative input files in CI to catch format drift.

Example fix

// before
float v = FloatParser.parseField(bytes, start, len, '|');

// after
String field = new String(bytes, start, len, StandardCharsets.UTF_8).trim();
float v = Float.parseFloat(field);
Defensive patterns

Strategy: validation

Validate before calling

int end = start + len;
if (len > 0 && (Character.isWhitespace(bytes[start]) || Character.isWhitespace(bytes[end - 1]))) {
    throw new IllegalArgumentException("Untrimmed float field");
}

Try / catch

try {
    float v = FloatParser.parseField(bytes, start, len, delim);
} catch (NumberFormatException e) {
    float v = Float.parseFloat(new String(bytes, start, len, StandardCharsets.UTF_8).trim());
}

Prevention

When it happens

Trigger: Calling FloatParser.parseField(bytes, startPos, length, delimiter) with fields like " 0.5f" or "1.5 " — padding or a trailing \r inside the column.

Common situations: Padded columns from CSV/spreadsheet exports; CRLF line endings leaving \r on the last field; test fixtures built with string concatenation adding stray spaces.

Related errors


AI-assisted analysis of apache/flink@2f3c205e92 (2026-08-14). Data as JSON: /api/errors/8dbfb17614ef8880. Report an issue: GitHub.