TheAlgorithms/Java · error · IllegalArgumentException
Input strings must not be null.
Error message
Input strings must not be null.
What it means
Thrown by NeedlemanWunsch.align(s1, s2, matchScore, mismatchPenalty, gapPenalty) when either s1 or s2 is null. The method builds a DP matrix sized by s1.length()/s2.length(), which would NPE on a null String. Both arguments are checked together and rejected with 'Input strings must not be null.'
Source
Thrown at src/main/java/com/thealgorithms/dynamicprogramming/NeedlemanWunsch.java:29
public final class NeedlemanWunsch {
private NeedlemanWunsch() {
// Utility Class
}
/**
* Computes the Needleman–Wunsch global alignment score between two strings.
*
* @param s1 the first string
* @param s2 the second string
* @param matchScore score for a character match
* @param mismatchPenalty penalty for a mismatch (should be negative)
* @param gapPenalty penalty for inserting a gap (should be negative)
* @return the optimal alignment score
*/
public static int align(String s1, String s2, int matchScore, int mismatchPenalty, int gapPenalty) {
if (s1 == null || s2 == null) {
throw new IllegalArgumentException("Input strings must not be null.");
}
int n = s1.length();
int m = s2.length();
int[][] dp = new int[n + 1][m + 1];
// Initialize gap penalties for first row and column
for (int i = 0; i <= n; i++) {
dp[i][0] = i * gapPenalty;
}
for (int j = 0; j <= m; j++) {
dp[0][j] = j * gapPenalty;
}
// Fill the DP matrix
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {View on GitHub (pinned to fdfb9a395b)
Solutions
- Validate both strings are non-null (and typically non-null + reasonable length) before calling align().
- Default missing sequences to empty strings if an empty-vs-empty alignment is meaningful for your case.
- Surface the missing input to the caller/user rather than letting it reach the algorithm.
Example fix
// before int score = NeedlemanWunsch.align(s1, s2, 1, -1, -2); // after Objects.requireNonNull(s1, "s1"); Objects.requireNonNull(s2, "s2"); int score = NeedlemanWunsch.align(s1, s2, 1, -1, -2);
Defensive patterns
Strategy: validation
Validate before calling
Objects.requireNonNull(s1, "s1"); Objects.requireNonNull(s2, "s2"); NeedlemanWunsch.align(s1, s2, matchScore, mismatchPenalty, gapPenalty);
Type guard
s1 != null && s2 != null
Prevention
- Wrap nullable sequence sources in Optional and require both present.
- Validate sequences at the request/service boundary before alignment.
- Keep a shared precondition helper for sequence-pair inputs.
When it happens
Trigger: Passing null for either sequence; one sequence sourced from a nullable field while the other is a literal; forgetting to validate after a list-to-string conversion that can return null.
Common situations: Bioinformatics pipelines loading sequences from files that may be missing; web requests where one of two sequence parameters was omitted.
Related errors
- Input strings must not be null.
- Input string must not be null
- Keys and frequencies cannot be null
- Price array cannot be null or empty.
- Input array cannot be null or empty.
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/06dee44545ca8705.
Report an issue: GitHub.