TheAlgorithms/Java · error · IllegalArgumentException

Input strings must not be null.

Error message

Input strings must not be null.

What it means

Thrown by SmithWaterman.align(s1, s2, matchScore, mismatchPenalty, gapPenalty) when either string is null. The local-alignment DP indexes s1.charAt/s2.charAt which would NPE on null; both are checked together. Message: 'Input strings must not be null.'

Source

Thrown at src/main/java/com/thealgorithms/dynamicprogramming/SmithWaterman.java:28

public final class SmithWaterman {

    private SmithWaterman() {
        // Utility Class
    }

    /**
     * Computes the Smith–Waterman local alignment score between two strings.
     *
     * @param s1 first string
     * @param s2 second string
     * @param matchScore score for a match
     * @param mismatchPenalty penalty for mismatch (negative)
     * @param gapPenalty penalty for insertion/deletion (negative)
     * @return the maximum local 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 maxScore = 0;

        int[][] dp = new int[n + 1][m + 1];

        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                int matchOrMismatch = (s1.charAt(i - 1) == s2.charAt(j - 1)) ? matchScore : mismatchPenalty;

                dp[i][j] = Math.max(0,
                    Math.max(Math.max(dp[i - 1][j - 1] + matchOrMismatch, // match/mismatch
                                 dp[i - 1][j] + gapPenalty // deletion
                                 ),
                        dp[i][j - 1] + gapPenalty // insertion
                        ));

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Require both sequences non-null (Objects.requireNonNull) before calling align.
  2. Treat a missing sequence as empty string only if local alignment of empty input is meaningful.
  3. Report missing input to the caller instead of letting it reach the algorithm.

Example fix

// before
int score = SmithWaterman.align(s1, s2, 2, -1, -1);

// after
Objects.requireNonNull(s1, "s1");
Objects.requireNonNull(s2, "s2");
int score = SmithWaterman.align(s1, s2, 2, -1, -1);
Defensive patterns

Strategy: validation

Validate before calling

Objects.requireNonNull(s1, "s1");
Objects.requireNonNull(s2, "s2");
SmithWaterman.align(s1, s2, matchScore, mismatchPenalty, gapPenalty);

Type guard

s1 != null && s2 != null

Prevention

When it happens

Trigger: Passing null for either sequence; one sequence loaded from a nullable source; chain where an earlier filter dropped the value to null.

Common situations: Bioinformatics tools where one sequence file is missing; REST endpoints receiving only one of two expected sequence fields.

Related errors


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