TheAlgorithms/Java · error · IllegalArgumentException

Input string cannot be null or empty

Error message

Input string cannot be null or empty

What it means

Thrown by HarshadNumber.isHarshad(String s) when s is null or empty (s.isEmpty()). This is the string-overload variant of the Harshad checker that parses the string to a long. The null/empty guard runs before Long.parseLong to avoid a NullPointerException or NumberFormatException on unparseable input.

Source

Thrown at src/main/java/com/thealgorithms/maths/HarshadNumber.java:57

        return n % sumOfDigits == 0;
    }

    /**
     * Checks if a number represented as a string is a Harshad number.
     * A Harshad number is a positive integer that is divisible by the sum of its
     * digits.
     *
     * @param s the string representation of the number to be checked
     * @return {@code true} if the number is a Harshad number, otherwise
     *         {@code false}
     * @throws IllegalArgumentException if {@code s} is null, empty, or represents a
     *                                  non-positive integer
     * @throws NumberFormatException    if {@code s} cannot be parsed as a long
     */
    public static boolean isHarshad(String s) {
        if (s == null || s.isEmpty()) {
            throw new IllegalArgumentException("Input string cannot be null or empty");
        }

        final long n;
        try {
            n = Long.parseLong(s);
        } catch (NumberFormatException e) {
            throw new IllegalArgumentException("Input string must be a valid integer: " + s, e);
        }

        if (n <= 0) {
            throw new IllegalArgumentException("Input must be a positive integer. Received: " + n);
        }

        int sumOfDigits = 0;
        for (char ch : s.toCharArray()) {
            if (Character.isDigit(ch)) {
                sumOfDigits += ch - '0';
            }

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Check for null and empty before calling isHarshad(String).
  2. Use a safe-input wrapper: if (s == null || s.isBlank()) return false.
  3. Validate string inputs at the data-ingestion boundary.

Example fix

// before
boolean result = HarshadNumber.isHarshad(userInput);

// after
if (userInput == null || userInput.isBlank()) {
    return false;
}
boolean result = HarshadNumber.isHarshad(userInput.trim());
Defensive patterns

Strategy: validation

Validate before calling

if (s == null || s.isEmpty()) {
    return false;
}
boolean result = HarshadNumber.isHarshad(s);

Type guard

static boolean isNonEmptyString(String s) {
    return s != null && !s.isEmpty();
}

Try / catch

try {
    boolean result = HarshadNumber.isHarshad(s);
} catch (IllegalArgumentException e) {
    // s was null or empty; handle gracefully
}

Prevention

When it happens

Trigger: Calling isHarshad((String) null), isHarshad(""), or isHarshad(new String()). A string containing only whitespace (e.g., " ") does NOT trigger this — it falls through to the NumberFormatException handler (error 413).

Common situations: User input fields that are blank or unsubmitted. JSON/XML deserialization producing null string fields. Data from APIs or files with missing values represented as null or empty strings.

Related errors


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