TheAlgorithms/Java · error · IllegalArgumentException

Input strings must not be null.

Error message

Input strings must not be null.

What it means

The DamerauLevenshtein distance algorithm builds a character-position map and iterates over both input strings, so a null reference would cause an NPE deep inside the computation. validateInputs checks both strings upfront and throws IllegalArgumentException with a clear message rather than letting a cryptic NullPointerException surface later.

Source

Thrown at src/main/java/com/thealgorithms/dynamicprogramming/DamerauLevenshteinDistance.java:56

        Map<Character, Integer> charLastPosition = buildCharacterMap(s1, s2);
        int[][] dp = initializeTable(n, m);

        fillTable(s1, s2, dp, charLastPosition);

        return dp[n + 1][m + 1];
    }

    /**
     * Validates that both input strings are not null.
     *
     * @param s1 the first string to validate
     * @param s2 the second string to validate
     * @throws IllegalArgumentException if either string is null
     */
    private static void validateInputs(String s1, String s2) {
        if (s1 == null || s2 == null) {
            throw new IllegalArgumentException("Input strings must not be null.");
        }
    }

    /**
     * Builds a character map containing all unique characters from both strings.
     * Each character is initialized with a position value of 0.
     *
     * This map is used to track the last occurrence position of each character
     * during the distance computation, which is essential for handling transpositions.
     *
     * @param s1 the first string
     * @param s2 the second string
     * @return a map containing all unique characters from both strings, initialized to 0
     */
    private static Map<Character, Integer> buildCharacterMap(String s1, String s2) {
        Map<Character, Integer> charMap = new HashMap<>();
        for (char c : s1.toCharArray()) {
            charMap.putIfAbsent(c, 0);

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Substitute null with an empty string ("") if an empty-vs-nonempty comparison is meaningful for your use case.
  2. Add a null check in the caller and skip the computation or return a sentinel value.
  3. Use Optional.ofNullable(str).orElse("") at the call site.

Example fix

// before
int d = DamerauLevenshteinDistance.compute(null, "hello"); // throws

// after
String s1 = Optional.ofNullable(rawS1).orElse("");
String s2 = Optional.ofNullable(rawS2).orElse("");
int d = DamerauLevenshteinDistance.compute(s1, s2);
Defensive patterns

Strategy: validation

Validate before calling

String s1Safe = (s1 != null) ? s1 : "";
String s2Safe = (s2 != null) ? s2 : "";
int dist = DamerauLevenshteinDistance.compute(s1Safe, s2Safe);

Type guard

static boolean bothNonNull(String s1, String s2) {
    return s1 != null && s2 != null;
}

Try / catch

try {
    dist = DamerauLevenshteinDistance.compute(s1, s2);
} catch (IllegalArgumentException e) {
    dist = -1; // or handle null case explicitly
}

Prevention

When it happens

Trigger: Calling the public distance method (which invokes validateInputs) with one or both String arguments being null — commonly from unmarshalled JSON, database columns that allow null, or optional user inputs.

Common situations: Processing form fields where one string is optional and left null; deserializing objects with nullable string fields; defaulting to null instead of empty string.

Related errors


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