perwendel/spark · error · java.lang.NumberFormatException

${s}

Error message

${s}

What it means

TypeUtil.parseInt throws NumberFormatException carrying the offending substring when a character inside the parsed range is not a valid digit for the requested base. It is the low-level hex/integer parser used by URL decoding in the spark utils.

Solutions

  1. Validate the substring characters are legal digits for the base before calling parseInt.
  2. Catch NumberFormatException around the parse and treat the input as a malformed URL/string.
  3. Sanitize/URL-encode the input at the source so only valid escape sequences arrive.
  4. Use a standard decoder (URLDecoder.decode) for user-supplied strings instead of the raw TypeUtil parser.

Example fix

// before
long v = TypeUtil.parseInt(s, 0, 2, 16);
// after
long v;
try {
    v = TypeUtil.parseInt(s, 0, 2, 16);
} catch (NumberFormatException e) {
    throw new IllegalArgumentException("Invalid hex digits: " + e.getMessage());
}
Defensive patterns

Strategy: try-catch

Validate before calling

boolean validForBase(String s, int offset, int length, int base) {
    for (int i = 0; i < length; i++) {
        if (Character.digit(s.charAt(offset + i), base) < 0) return false;
    }
    return true;
}

Try / catch

try {
    long v = TypeUtil.parseInt(s, offset, length, base);
} catch (NumberFormatException e) {
    throw new IllegalArgumentException("Invalid digits: " + e.getMessage());
}

Prevention

When it happens

Trigger: Calling TypeUtil.parseInt(s, offset, length, base) where any character in s.substring(offset, offset+length) is outside the digit range for the given base, e.g. parsing a non-hex character with base 16, or a slice of invalid characters.

Common situations: Malformed percent-encoded URLs where the two characters after '%' are not hex digits; corrupted query strings; calling the parser directly with hand-crafted substrings of wrong length.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


AI-assisted analysis of perwendel/spark@1973e402f5 (2026-09-10). Data as JSON: /api/errors/c9a28e8ab4557524. Report an issue: GitHub.

Appendix: source

Thrown at src/main/java/spark/utils/urldecoding/TypeUtil.java:174

     * @param length Length of integer or -1 for remainder of string
     * @param base   base of the integer
     * @return the parsed integer
     * @throws NumberFormatException if the string cannot be parsed
     */
    public static int parseInt(String s, int offset, int length, int base)
        throws NumberFormatException {
        int value = 0;

        if (length < 0) {
            length = s.length() - offset;
        }

        for (int i = 0; i < length; i++) {
            char c = s.charAt(offset + i);

            int digit = convertHexDigit((int) c);
            if (digit < 0 || digit >= base) {
                throw new NumberFormatException(s.substring(offset, offset + length));
            }
            value = value * base + digit;
        }
        return value;
    }

    /* ------------------------------------------------------------ */
    public static String toString(byte[] bytes, int base) {
        StringBuilder buf = new StringBuilder();
        for (byte b : bytes) {
            int bi = 0xff & b;
            int c = '0' + (bi / base) % base;
            if (c > '9') {
                c = 'a' + (c - '0' - 10);
            }
            buf.append((char) c);
            c = '0' + bi % base;
            if (c > '9') {

View on GitHub (pinned to 1973e402f5)