TheAlgorithms/Java · error · IllegalArgumentException

Input cannot be null or empty

Error message

Input cannot be null or empty

What it means

Thrown by OctalToDecimal.convertOctalToDecimal when the inputOctal string is null or empty. This is the first guard before the character-by-character parsing loop begins. An empty string has no digits to process, and a null string would cause a NullPointerException in the loop.

Source

Thrown at src/main/java/com/thealgorithms/conversions/OctalToDecimal.java:24

 */
public final class OctalToDecimal {
    private static final int OCTAL_BASE = 8;

    private OctalToDecimal() {
    }

    /**
     * Converts a given octal number (as a string) to its decimal representation.
     * If the input is not a valid octal number (i.e., contains characters other than 0-7),
     * the method throws an IllegalArgumentException.
     *
     * @param inputOctal The octal number as a string
     * @return The decimal equivalent of the octal number
     * @throws IllegalArgumentException if the input is not a valid octal number
     */
    public static int convertOctalToDecimal(String inputOctal) {
        if (inputOctal == null || inputOctal.isEmpty()) {
            throw new IllegalArgumentException("Input cannot be null or empty");
        }

        int decimalValue = 0;

        for (int i = 0; i < inputOctal.length(); i++) {
            char currentChar = inputOctal.charAt(i);

            if (currentChar < '0' || currentChar > '7') {
                throw new IllegalArgumentException("Incorrect input: Expecting an octal number (digits 0-7)");
            }

            int currentDigit = currentChar - '0';
            decimalValue = decimalValue * OCTAL_BASE + currentDigit;
        }

        return decimalValue;
    }
}

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Check for null or empty before calling convertOctalToDecimal and handle the case explicitly.
  2. If empty input should map to zero, return 0 yourself instead of relying on the library.
  3. Use a utility like StringUtils from Apache Commons to reject blank strings at the boundary.

Example fix

// before
int result = OctalToDecimal.convertOctalToDecimal(octalStr);

// after
if (octalStr == null || octalStr.isEmpty()) {
    throw new IllegalArgumentException("Octal input must not be null or empty");
    // or: return 0; // if empty should mean zero
}
int result = OctalToDecimal.convertOctalToDecimal(octalStr);
Defensive patterns

Strategy: validation

Validate before calling

if (inputOctal == null || inputOctal.isEmpty()) {
    throw new IllegalArgumentException("Octal input must not be null or empty");
}
int result = OctalToDecimal.convertOctalToDecimal(inputOctal);

Type guard

static boolean isValidOctalInput(String s) {
    return s != null && !s.isEmpty() && s.matches("^[0-7]+$");
}

Try / catch

try {
    int result = OctalToDecimal.convertOctalToDecimal(inputOctal);
} catch (IllegalArgumentException e) {
    // null, empty, or invalid octal input; handle gracefully
    logger.warn("Invalid octal input: {}", inputOctal);
}

Prevention

When it happens

Trigger: Calling convertOctalToDecimal(null) or convertOctalToDecimal(""). Passing a variable that was not initialized or whose source returned an empty value. Providing an empty field from user input or a parsed data file.

Common situations: A form field for octal input is left blank. A file/CSV column is empty for some rows. A function parameter is conditionally set and the condition was not met, leaving the string null.

Related errors


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