{"record":{"id":"ffa45ef889c26202","repo":"TheAlgorithms/Java","slug":"input-string-must-not-be-null","errorCode":null,"errorMessage":"Input string must not be null","messagePattern":"Input string must not be null","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/dynamicprogramming/LongestPalindromicSubsequence.java","lineNumber":24,"sourceCode":" * This implementation finds the longest such subsequence by computing the LCS of the\n * original string and its reverse.\n *\n * @see <a href=\"https://en.wikipedia.org/wiki/Longest_palindromic_subsequence\">Wikipedia</a>\n */\npublic final class LongestPalindromicSubsequence {\n    private LongestPalindromicSubsequence() {\n    }\n\n    /**\n     * Returns the longest palindromic subsequence of the given string.\n     *\n     * @param original the input string\n     * @return the longest palindromic subsequence\n     * @throws IllegalArgumentException if the input string is null\n     */\n    public static String lps(String original) {\n        if (original == null) {\n            throw new IllegalArgumentException(\"Input string must not be null\");\n        }\n        String reverse = new StringBuilder(original).reverse().toString();\n        return recursiveLPS(original, reverse);\n    }\n\n    private static String recursiveLPS(String original, String reverse) {\n        if (original.isEmpty() || reverse.isEmpty()) {\n            return \"\";\n        }\n        if (original.charAt(original.length() - 1) == reverse.charAt(reverse.length() - 1)) {\n            String bestSubResult = recursiveLPS(original.substring(0, original.length() - 1), reverse.substring(0, reverse.length() - 1));\n            return reverse.charAt(reverse.length() - 1) + bestSubResult;\n        }\n        String sub1 = recursiveLPS(original, reverse.substring(0, reverse.length() - 1));\n        String sub2 = recursiveLPS(original.substring(0, original.length() - 1), reverse);\n        return sub1.length() >= sub2.length() ? sub1 : sub2;\n    }\n}","sourceCodeStart":6,"sourceCodeEnd":42,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/dynamicprogramming/LongestPalindromicSubsequence.java#L6-L42","documentation":"Thrown by LongestPalindromicSubsequence.lps(String) when the input string is null. The method computes the longest palindromic subsequence by reversing the input and finding the LCS; calling StringBuilder.reverse() on null would NPE, so the library rejects null explicitly with IllegalArgumentException. The message is 'Input string must not be null'.","triggerScenarios":"Calling lps(null); passing a String variable that was never initialized or came from a map.get()/Optional that returned null; piping a nullable field from a data class directly into lps().","commonSituations":"Parsing user input that may be blank/absent; reading from a database column that allows null; deserializing JSON where the field is omitted and mapped to null.","solutions":["Null-check the input before calling lps(): return early or supply a default (e.g. empty string).","Ensure the upstream source never produces null (use Optional, default values, or @NotNull validation).","Wrap the call in try/catch(IllegalArgumentException) as a last-resort guard if nullability is unavoidable."],"exampleFix":"// before\nString result = LongestPalindromicSubsequence.lps(maybeNull);\n\n// after\nif (original == null) {\n    throw new IllegalArgumentException(\"original must not be null\", e);\n}\nString result = LongestPalindromicSubsequence.lps(original);","handlingStrategy":"validation","validationCode":"if (original == null) {\n    throw new IllegalArgumentException(\"original must not be null\");\n}\nString lps = LongestPalindromicSubsequence.lps(original);","typeGuard":"original != null","tryCatchPattern":null,"preventionTips":["Annotate the parameter with @NotNull and enable static analysis.","Treat empty string as the valid 'no input' case rather than null.","Centralize string-input validation at the service boundary."],"tags":["null-check","string","dynamic-programming","input-validation"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}