TheAlgorithms/Java · error · WordsToNumberException

NULL_INPUT

NULL_INPUT

Error message

Invalid Input. 'null' or empty input provided

What it means

Thrown by WordsToNumber.convert when numberInWords is null (the very first guard). The message reads 'null or empty input' but this specific site only fires on a literal null reference. An empty/whitespace string is caught later in preprocessWords (error 104).

Source

Thrown at src/main/java/com/thealgorithms/conversions/WordsToNumber.java:98

        PowerOfTen(String word, BigDecimal value) {
            this.word = word;
            this.value = value;
        }

        public static BigDecimal getValue(String word) {
            for (PowerOfTen power : values()) {
                if (word.equals(power.word)) {
                    return power.value;
                }
            }
            return null;
        }
    }

    public static String convert(String numberInWords) {
        if (numberInWords == null) {
            throw new WordsToNumberException(WordsToNumberException.ErrorType.NULL_INPUT, "");
        }

        ArrayDeque<String> wordDeque = preprocessWords(numberInWords);
        BigDecimal completeNumber = convertWordQueueToBigDecimal(wordDeque);

        return completeNumber.toString();
    }

    public static BigDecimal convertToBigDecimal(String numberInWords) {
        String conversionResult = convert(numberInWords);
        return new BigDecimal(conversionResult);
    }

    private static ArrayDeque<String> preprocessWords(String numberInWords) {
        String[] wordSplitArray = numberInWords.trim().split("[ ,-]");
        ArrayDeque<String> wordDeque = new ArrayDeque<>();
        for (String word : wordSplitArray) {
            if (word.isEmpty()) {

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Null-check the input before calling convert.
  2. Use Optional or a default value for the input string.
  3. Validate at the boundary where the string enters your code.

Example fix

// before
String result = WordsToNumber.convert(maybeNullInput);
// after
String result = WordsToNumber.convert(
    Objects.requireNonNullElseGet(input, () -> { throw new IllegalArgumentException("input required"); }));
Defensive patterns

Strategy: validation

Validate before calling

static String convertSafe(String input) {
    if (input == null) throw new IllegalArgumentException("numberInWords must not be null");
    return WordsToNumber.convert(input);
}

Type guard

static boolean isNonNullOrBlank(String s) {
    return s != null;
}

Try / catch

try {
    return WordsToNumber.convert(input);
} catch (WordsToNumberException e) {
    if (e.errorType == ErrorType.NULL_INPUT) throw new ApiException(400, "input is required");
    throw e;
}

Prevention

When it happens

Trigger: Passing null explicitly: convert(null). Passing a variable that was never initialized or came from a map.get() that returned null.

Common situations: Unboxing results from optional/map lookups without null checks; deserialization producing null fields; optional user input fields that were left blank and defaulted to null.

Related errors


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