TheAlgorithms/Java · error · NullPointerException
Input lists and result collection must not be null.
Error message
Input lists and result collection must not be null.
What it means
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.
Source
Thrown at src/main/java/com/thealgorithms/datastructures/lists/MergeSortedArrayList.java:48
public final class MergeSortedArrayList {
private MergeSortedArrayList() {
}
/**
* Merges two sorted lists of integers into a single sorted collection.
*
* <p>This method does not alter the original lists (`listA` and `listB`). Instead, it inserts elements from both
* lists into `listC` in a way that maintains ascending order.</p>
*
* @param listA The first sorted list of integers.
* @param listB The second sorted list of integers.
* @param listC The collection to hold the merged result, maintaining sorted order.
* @throws NullPointerException if any of the input lists or result collection is null.
*/
public static void merge(List<Integer> listA, List<Integer> listB, Collection<Integer> listC) {
if (listA == null || listB == null || listC == null) {
throw new NullPointerException("Input lists and result collection must not be null.");
}
int pa = 0;
int pb = 0;
while (pa < listA.size() && pb < listB.size()) {
if (listA.get(pa) <= listB.get(pb)) {
listC.add(listA.get(pa++));
} else {
listC.add(listB.get(pb++));
}
}
// Add remaining elements from listA, if any
while (pa < listA.size()) {
listC.add(listA.get(pa++));
}
// Add remaining elements from listB, if anyView on GitHub (pinned to fdfb9a395b)
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.
Example fix
// before
MergeSortedArrayList.merge(listA, listB, null);
// after
List<Integer> result = new ArrayList<>();
MergeSortedArrayList.merge(
listA != null ? listA : Collections.emptyList(),
listB != null ? listB : Collections.emptyList(),
result); Defensive patterns
Strategy: validation
Validate before calling
if (listA != null && listB != null && listC != null) {
MergeSortedArrayList.merge(listA, listB, listC);
} Try / catch
try {
MergeSortedArrayList.merge(listA, listB, listC);
} catch (NullPointerException e) {
// one of the arguments was null
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Input lists must not be null.
- Cannot insert null element
- Cannot add null element to the list
- Cannot add null element to the list
- Element cannot be null
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/f602b785b7ea6990.
Report an issue: GitHub.