TheAlgorithms/Java · error · IllegalArgumentException
Input string must not be null
Error message
Input string must not be null
What it means
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'.
Source
Thrown at src/main/java/com/thealgorithms/dynamicprogramming/LongestPalindromicSubsequence.java:24
* This implementation finds the longest such subsequence by computing the LCS of the
* original string and its reverse.
*
* @see <a href="https://en.wikipedia.org/wiki/Longest_palindromic_subsequence">Wikipedia</a>
*/
public final class LongestPalindromicSubsequence {
private LongestPalindromicSubsequence() {
}
/**
* Returns the longest palindromic subsequence of the given string.
*
* @param original the input string
* @return the longest palindromic subsequence
* @throws IllegalArgumentException if the input string is null
*/
public static String lps(String original) {
if (original == null) {
throw new IllegalArgumentException("Input string must not be null");
}
String reverse = new StringBuilder(original).reverse().toString();
return recursiveLPS(original, reverse);
}
private static String recursiveLPS(String original, String reverse) {
if (original.isEmpty() || reverse.isEmpty()) {
return "";
}
if (original.charAt(original.length() - 1) == reverse.charAt(reverse.length() - 1)) {
String bestSubResult = recursiveLPS(original.substring(0, original.length() - 1), reverse.substring(0, reverse.length() - 1));
return reverse.charAt(reverse.length() - 1) + bestSubResult;
}
String sub1 = recursiveLPS(original, reverse.substring(0, reverse.length() - 1));
String sub2 = recursiveLPS(original.substring(0, original.length() - 1), reverse);
return sub1.length() >= sub2.length() ? sub1 : sub2;
}
}View on GitHub (pinned to fdfb9a395b)
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.
Example fix
// before
String result = LongestPalindromicSubsequence.lps(maybeNull);
// after
if (original == null) {
throw new IllegalArgumentException("original must not be null", e);
}
String result = LongestPalindromicSubsequence.lps(original); Defensive patterns
Strategy: validation
Validate before calling
if (original == null) {
throw new IllegalArgumentException("original must not be null");
}
String lps = LongestPalindromicSubsequence.lps(original); Type guard
original != null
Prevention
- 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.
When it happens
Trigger: 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().
Common situations: 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.
Related errors
- Input strings must not be null.
- Keys and frequencies cannot be null
- Price array cannot be null or empty.
- Input strings must not be null.
- Input array cannot be null or empty.
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/ffa45ef889c26202.
Report an issue: GitHub.