TheAlgorithms/Java · error · NumberFormatException

Input parameter must not be null!

Error message

Input parameter must not be null!

What it means

Thrown by ParseInteger.checkInput (a private helper) when the input string s is null. The method deliberately throws NumberFormatException (mirroring Integer.parseInt semantics) so callers using standard numeric-parsing exception handling are consistent. The null check is the first guard before any string operation.

Source

Thrown at src/main/java/com/thealgorithms/maths/ParseInteger.java:9

package com.thealgorithms.maths;

public final class ParseInteger {
    private ParseInteger() {
    }

    private static void checkInput(final String s) {
        if (s == null) {
            throw new NumberFormatException("Input parameter must not be null!");
        }
        if (s.isEmpty()) {
            throw new NumberFormatException("Input parameter must not be empty!");
        }
    }

    private static void checkDigitAt(final String s, final int pos) {
        if (!Character.isDigit(s.charAt(pos))) {
            throw new NumberFormatException("Input parameter of incorrect format: " + s);
        }
    }

    private static int digitToInt(final char digit) {
        return digit - '0';
    }

    /**
     * Parse a string to integer

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Null-check at the source: use Objects.requireNonNull(s, ...) or an explicit if (s == null) guard.
  2. Coalesce null to a default or reject the request before calling parse.
  3. Use Optional.ofNullable(s).map(ParseInteger::parse) to handle absence explicitly.

Example fix

// before
int v = ParseInteger.parse(input);

// after
Integer v = input == null ? null : ParseInteger.parse(input);
// or reject:
Objects.requireNonNull(input, "input must not be null");
int v = ParseInteger.parse(input);
Defensive patterns

Strategy: validation

Validate before calling

Objects.requireNonNull(s, "input must not be null");
int v = ParseInteger.parse(s);

Try / catch

try {
    int v = ParseInteger.parse(s);
} catch (NumberFormatException e) {
    // handle null, empty, or malformed input uniformly
    throw new IllegalArgumentException("invalid integer input: " + s, e);
}

Prevention

When it happens

Trigger: Calling the public parse method (which delegates to checkInput) with a null String argument — e.g., ParseInteger's parse(null).

Common situations: JSON/CSV field mapped to null due to missing key; Optional/path that resolved to null; variable default-initialized to null and never assigned; refactoring that removed an intermediate null-check.

Related errors


AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13). Data as JSON: /api/errors/c6709cdb83f26fce. Report an issue: GitHub.