{"record":{"id":"06dee44545ca8705","repo":"TheAlgorithms/Java","slug":"input-strings-must-not-be-null-06dee4","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/NeedlemanWunsch.java","lineNumber":29,"sourceCode":"public final class NeedlemanWunsch {\n\n    private NeedlemanWunsch() {\n        // Utility Class\n    }\n\n    /**\n     * Computes the Needleman–Wunsch global alignment score between two strings.\n     *\n     * @param s1 the first string\n     * @param s2 the second string\n     * @param matchScore score for a character match\n     * @param mismatchPenalty penalty for a mismatch (should be negative)\n     * @param gapPenalty penalty for inserting a gap (should be negative)\n     * @return the optimal 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\n        int[][] dp = new int[n + 1][m + 1];\n\n        // Initialize gap penalties for first row and column\n        for (int i = 0; i <= n; i++) {\n            dp[i][0] = i * gapPenalty;\n        }\n        for (int j = 0; j <= m; j++) {\n            dp[0][j] = j * gapPenalty;\n        }\n\n        // Fill the DP matrix\n        for (int i = 1; i <= n; i++) {\n            for (int j = 1; j <= m; j++) {","sourceCodeStart":11,"sourceCodeEnd":47,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/dynamicprogramming/NeedlemanWunsch.java#L11-L47","documentation":"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.'","triggerScenarios":"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.","commonSituations":"Bioinformatics pipelines loading sequences from files that may be missing; web requests where one of two sequence parameters was omitted.","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."],"exampleFix":"// before\nint score = NeedlemanWunsch.align(s1, s2, 1, -1, -2);\n\n// after\nObjects.requireNonNull(s1, \"s1\");\nObjects.requireNonNull(s2, \"s2\");\nint score = NeedlemanWunsch.align(s1, s2, 1, -1, -2);","handlingStrategy":"validation","validationCode":"Objects.requireNonNull(s1, \"s1\");\nObjects.requireNonNull(s2, \"s2\");\nNeedlemanWunsch.align(s1, s2, matchScore, mismatchPenalty, gapPenalty);","typeGuard":"s1 != null && s2 != null","tryCatchPattern":null,"preventionTips":["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."],"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"}