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 DoubleParser.parseField when a double text field has leading or trailing whitespace. The parser measures the field up to the delimiter with nextStringLength, checks the first and last bytes with Character.isWhitespace, and rejects before handing the string to Double.parseDouble (which would not accept the padding either).

Source

Thrown at flink-core/src/main/java/org/apache/flink/types/parser/DoubleParser.java:101

    /**
     * Static utility to parse a field of type double 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 double 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 Double.parseDouble(str);
    }
}

View on GitHub (pinned to 2f3c205e92)

Solutions

  1. Trim the field before parsing, or clean the input so numeric columns carry no padding.
  2. Normalize line endings (\r\n -> \n) for files sourced from Windows systems.
  3. Add fixture-based tests using real exported files to catch formatting drift.

Example fix

// before
double v = DoubleParser.parseField(bytes, start, len, '|');

// after
String field = new String(bytes, start, len, StandardCharsets.UTF_8).trim();
double v = Double.parseDouble(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 double field");
}

Try / catch

try {
    double v = DoubleParser.parseField(bytes, start, len, delim);
} catch (NumberFormatException e) {
    double v = Double.parseDouble(new String(bytes, start, len, StandardCharsets.UTF_8).trim());
}

Prevention

When it happens

Trigger: Calling DoubleParser.parseField(bytes, startPos, length, delimiter) on fields like " 1.5" or "2.5e3 " — padding inside the delimited column.

Common situations: Spreadsheet/CSV exports with padded columns; trailing \r from CRLF line endings on the last field; fixed-width extracts sliced with leading spaces included.

Related errors


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