{"record":{"id":"eec4cee37bc51dc6","repo":"TheAlgorithms/Java","slug":"input-list-cannot-be-null","errorCode":null,"errorMessage":"Input list cannot be null","messagePattern":"Input list cannot be null","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/datastructures/heaps/MaxHeap.java","lineNumber":43,"sourceCode":" * </pre>\n *\n * @author Nicolas Renard\n */\npublic class MaxHeap implements Heap {\n\n    /** The internal list that stores heap elements */\n    private final List<HeapElement> maxHeap;\n\n    /**\n     * Constructs a new MaxHeap from a list of elements.\n     * Null elements in the input list are ignored.\n     *\n     * @param listElements List of HeapElement objects to initialize the heap\n     * @throws IllegalArgumentException if the input list is null\n     */\n    public MaxHeap(List<HeapElement> listElements) {\n        if (listElements == null) {\n            throw new IllegalArgumentException(\"Input list cannot be null\");\n        }\n\n        maxHeap = new ArrayList<>();\n\n        // Safe initialization: directly add non-null elements first\n        for (HeapElement heapElement : listElements) {\n            if (heapElement != null) {\n                maxHeap.add(heapElement);\n            }\n        }\n\n        // Heapify the array bottom-up\n        for (int i = maxHeap.size() / 2; i >= 0; i--) {\n            heapifyDown(i + 1); // +1 because heapifyDown expects 1-based index\n        }\n    }\n\n    /**","sourceCodeStart":25,"sourceCodeEnd":61,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/heaps/MaxHeap.java#L25-L61","documentation":"Thrown by the MaxHeap(List<HeapElement>) constructor when listElements is null. The constructor iterates the list to build the heap, so a null reference would NPE; the explicit check produces a clearer error. Note null ELEMENTS inside a non-null list are silently skipped (not an error).","triggerScenarios":"Passing null directly; passing the result of a factory/method that returns null on empty input; an uninitialized List field defaulting to null.","commonSituations":"DI/bean wiring that fails to inject the list; a repository/query method returning null instead of an empty list; conditional initialization that leaves the field null.","solutions":["Pass an empty list (Collections.emptyList()) instead of null when there is nothing to seed.","Guard the caller with a null check and default to an empty list.","Fix the upstream producer to never return null collections."],"exampleFix":"// before\nnew MaxHeap(repository.findAll()); // returns null when empty\n\n// after\nList<HeapElement> elems = repository.findAll();\nnew MaxHeap(elems != null ? elems : Collections.emptyList());","handlingStrategy":"validation","validationCode":"List<HeapElement> elems = source != null ? source : Collections.emptyList();\nnew MaxHeap(elems);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never pass null where a collection is expected; use Collections.emptyList().","Fix producers (repositories, queries) to return empty lists, not null.","Annotate parameters @Nonnull and enable null-analysis."],"tags":["heap","max-heap","null-check","constructor"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}