{"record":{"id":"e5b8a12516dc46a3","repo":"TheAlgorithms/Java","slug":"input-strings-must-not-be-null-e5b8a1","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/SmithWaterman.java","lineNumber":28,"sourceCode":"public final class SmithWaterman {\n\n    private SmithWaterman() {\n        // Utility Class\n    }\n\n    /**\n     * Computes the Smith–Waterman local alignment score between two strings.\n     *\n     * @param s1 first string\n     * @param s2 second string\n     * @param matchScore score for a match\n     * @param mismatchPenalty penalty for mismatch (negative)\n     * @param gapPenalty penalty for insertion/deletion (negative)\n     * @return the maximum local alignment score\n     */\n    public static int align(String s1, String s2, int matchScore, int mismatchPenalty, int gapPenalty) {\n        if (s1 == null || s2 == null) {\n            throw new IllegalArgumentException(\"Input strings must not be null.\");\n        }\n\n        int n = s1.length();\n        int m = s2.length();\n        int maxScore = 0;\n\n        int[][] dp = new int[n + 1][m + 1];\n\n        for (int i = 1; i <= n; i++) {\n            for (int j = 1; j <= m; j++) {\n                int matchOrMismatch = (s1.charAt(i - 1) == s2.charAt(j - 1)) ? matchScore : mismatchPenalty;\n\n                dp[i][j] = Math.max(0,\n                    Math.max(Math.max(dp[i - 1][j - 1] + matchOrMismatch, // match/mismatch\n                                 dp[i - 1][j] + gapPenalty // deletion\n                                 ),\n                        dp[i][j - 1] + gapPenalty // insertion\n                        ));","sourceCodeStart":10,"sourceCodeEnd":46,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/dynamicprogramming/SmithWaterman.java#L10-L46","documentation":"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.'","triggerScenarios":"Passing null for either sequence; one sequence loaded from a nullable source; chain where an earlier filter dropped the value to null.","commonSituations":"Bioinformatics tools where one sequence file is missing; REST endpoints receiving only one of two expected sequence fields.","solutions":["Require both sequences non-null (Objects.requireNonNull) before calling align.","Treat a missing sequence as empty string only if local alignment of empty input is meaningful.","Report missing input to the caller instead of letting it reach the algorithm."],"exampleFix":"// before\nint score = SmithWaterman.align(s1, s2, 2, -1, -1);\n\n// after\nObjects.requireNonNull(s1, \"s1\");\nObjects.requireNonNull(s2, \"s2\");\nint score = SmithWaterman.align(s1, s2, 2, -1, -1);","handlingStrategy":"validation","validationCode":"Objects.requireNonNull(s1, \"s1\");\nObjects.requireNonNull(s2, \"s2\");\nSmithWaterman.align(s1, s2, matchScore, mismatchPenalty, gapPenalty);","typeGuard":"s1 != null && s2 != null","tryCatchPattern":null,"preventionTips":["Require both sequences at the request boundary.","Use Optional to chain only when both are present.","Share a sequence-pair precondition helper across alignment tools."],"tags":["null-check","string","dynamic-programming","sequence-alignment"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}