{"record":{"id":"eb555045d27ce630","repo":"TheAlgorithms/Java","slug":"input-strings-must-not-be-null","errorCode":null,"errorMessage":"Input strings must not be null.","messagePattern":"Input strings must not be null\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/dynamicprogramming/DamerauLevenshteinDistance.java","lineNumber":56,"sourceCode":"\n        Map<Character, Integer> charLastPosition = buildCharacterMap(s1, s2);\n        int[][] dp = initializeTable(n, m);\n\n        fillTable(s1, s2, dp, charLastPosition);\n\n        return dp[n + 1][m + 1];\n    }\n\n    /**\n     * Validates that both input strings are not null.\n     *\n     * @param s1 the first string to validate\n     * @param s2 the second string to validate\n     * @throws IllegalArgumentException if either string is null\n     */\n    private static void validateInputs(String s1, String s2) {\n        if (s1 == null || s2 == null) {\n            throw new IllegalArgumentException(\"Input strings must not be null.\");\n        }\n    }\n\n    /**\n     * Builds a character map containing all unique characters from both strings.\n     * Each character is initialized with a position value of 0.\n     *\n     * This map is used to track the last occurrence position of each character\n     * during the distance computation, which is essential for handling transpositions.\n     *\n     * @param s1 the first string\n     * @param s2 the second string\n     * @return a map containing all unique characters from both strings, initialized to 0\n     */\n    private static Map<Character, Integer> buildCharacterMap(String s1, String s2) {\n        Map<Character, Integer> charMap = new HashMap<>();\n        for (char c : s1.toCharArray()) {\n            charMap.putIfAbsent(c, 0);","sourceCodeStart":38,"sourceCodeEnd":74,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/dynamicprogramming/DamerauLevenshteinDistance.java#L38-L74","documentation":"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.","triggerScenarios":"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.","commonSituations":"Processing form fields where one string is optional and left null; deserializing objects with nullable string fields; defaulting to null instead of empty string.","solutions":["Substitute null with an empty string (\"\") if an empty-vs-nonempty comparison is meaningful for your use case.","Add a null check in the caller and skip the computation or return a sentinel value.","Use Optional.ofNullable(str).orElse(\"\") at the call site."],"exampleFix":"// before\nint d = DamerauLevenshteinDistance.compute(null, \"hello\"); // throws\n\n// after\nString s1 = Optional.ofNullable(rawS1).orElse(\"\");\nString s2 = Optional.ofNullable(rawS2).orElse(\"\");\nint d = DamerauLevenshteinDistance.compute(s1, s2);","handlingStrategy":"validation","validationCode":"String s1Safe = (s1 != null) ? s1 : \"\";\nString s2Safe = (s2 != null) ? s2 : \"\";\nint dist = DamerauLevenshteinDistance.compute(s1Safe, s2Safe);","typeGuard":"static boolean bothNonNull(String s1, String s2) {\n    return s1 != null && s2 != null;\n}","tryCatchPattern":"try {\n    dist = DamerauLevenshteinDistance.compute(s1, s2);\n} catch (IllegalArgumentException e) {\n    dist = -1; // or handle null case explicitly\n}","preventionTips":["Normalize nullable strings to empty strings at the system boundary.","Use Optional<String> in your API signatures to make nullability explicit."],"tags":["string-distance","null-check","input-validation","java"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}