{"record":{"id":"f602b785b7ea6990","repo":"TheAlgorithms/Java","slug":"input-lists-and-result-collection-must-not-be-null","errorCode":null,"errorMessage":"Input lists and result collection must not be null.","messagePattern":"Input lists and result collection must not be null\\.","errorType":"exception","errorClass":"NullPointerException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/datastructures/lists/MergeSortedArrayList.java","lineNumber":48,"sourceCode":"public final class MergeSortedArrayList {\n\n    private MergeSortedArrayList() {\n    }\n\n    /**\n     * Merges two sorted lists of integers into a single sorted collection.\n     *\n     * <p>This method does not alter the original lists (`listA` and `listB`). Instead, it inserts elements from both\n     * lists into `listC` in a way that maintains ascending order.</p>\n     *\n     * @param listA The first sorted list of integers.\n     * @param listB The second sorted list of integers.\n     * @param listC The collection to hold the merged result, maintaining sorted order.\n     * @throws NullPointerException if any of the input lists or result collection is null.\n     */\n    public static void merge(List<Integer> listA, List<Integer> listB, Collection<Integer> listC) {\n        if (listA == null || listB == null || listC == null) {\n            throw new NullPointerException(\"Input lists and result collection must not be null.\");\n        }\n\n        int pa = 0;\n        int pb = 0;\n\n        while (pa < listA.size() && pb < listB.size()) {\n            if (listA.get(pa) <= listB.get(pb)) {\n                listC.add(listA.get(pa++));\n            } else {\n                listC.add(listB.get(pb++));\n            }\n        }\n\n        // Add remaining elements from listA, if any\n        while (pa < listA.size()) {\n            listC.add(listA.get(pa++));\n        }\n        // Add remaining elements from listB, if any","sourceCodeStart":30,"sourceCodeEnd":66,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/lists/MergeSortedArrayList.java#L30-L66","documentation":"Thrown by MergeSortedArrayList.merge(listA, listB, listC) when any of the three arguments is null. The merge walks both input lists with index pointers and writes into listC, so a null input or output would NPE during the merge. The library fails fast with NullPointerException listing all three arguments.","triggerScenarios":"Passing a null for listA, listB, or listC. One list being the result of a computation that returned null on empty input. Forgetting to initialize the result collection.","commonSituations":"listC left uninitialized (just declared, not constructed). One input list sourced from a nullable getter. Collections.emptyList() is fine, but an actual null reference is not.","solutions":["Ensure all three arguments are non-null: use Collections.emptyList() instead of null for missing inputs.","Initialize listC as a concrete collection (e.g., new ArrayList<>()) before passing.","Wrap the merge call in a null-check helper that substitutes empty lists.","Audit the data pipeline to eliminate null list references at the source."],"exampleFix":"// before\nMergeSortedArrayList.merge(listA, listB, null);\n\n// after\nList<Integer> result = new ArrayList<>();\nMergeSortedArrayList.merge(\n    listA != null ? listA : Collections.emptyList(),\n    listB != null ? listB : Collections.emptyList(),\n    result);","handlingStrategy":"validation","validationCode":"if (listA != null && listB != null && listC != null) {\n    MergeSortedArrayList.merge(listA, listB, listC);\n}","typeGuard":null,"tryCatchPattern":"try {\n    MergeSortedArrayList.merge(listA, listB, listC);\n} catch (NullPointerException e) {\n    // one of the arguments was null\n}","preventionTips":["Use Collections.emptyList() instead of null for missing inputs.","Always initialize the result collection before passing it.","Null-check all three arguments in a helper wrapper."],"tags":["merge","null-check","data-structure","precondition","java"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}