{"record":{"id":"69af2479034c2865","repo":"TheAlgorithms/Java","slug":"input-list-cannot-be-null-69af24","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/MinHeap.java","lineNumber":42,"sourceCode":" * HeapElement min = heap.getElement(); // Returns and removes the minimum element\n * ```\n *\n * @author Nicolas Renard\n */\npublic class MinHeap implements Heap {\n\n    private final List<HeapElement> minHeap;\n\n    /**\n     * Constructs a new MinHeap from a list of elements.\n     * Null elements in the input list are ignored with a warning message.\n     *\n     * @param listElements List of HeapElement objects to initialize the heap\n     * @throws IllegalArgumentException if the input list is null\n     */\n    public MinHeap(List<HeapElement> listElements) {\n        if (listElements == null) {\n            throw new IllegalArgumentException(\"Input list cannot be null\");\n        }\n\n        minHeap = new ArrayList<>();\n\n        // Safe initialization: directly add elements first\n        for (HeapElement heapElement : listElements) {\n            if (heapElement != null) {\n                minHeap.add(heapElement);\n            } else {\n                System.out.println(\"Null element. Not added to heap\");\n            }\n        }\n\n        // Heapify the array bottom-up\n        for (int i = minHeap.size() / 2; i >= 0; i--) {\n            heapifyDown(i + 1);\n        }\n","sourceCodeStart":24,"sourceCodeEnd":60,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/heaps/MinHeap.java#L24-L60","documentation":"Thrown by the MinHeap(List<HeapElement>) constructor when listElements is null. The constructor iterates the list to seed the heap, so null would NPE; the explicit check yields a clearer message. Unlike MaxHeap's constructor, MinHeap prints a warning ('Null element. Not added to heap') for null ELEMENTS inside a non-null list but still rejects a null list reference.","triggerScenarios":"Passing null directly; passing a factory result that returns null on empty; an uninitialized List field.","commonSituations":"Repository/query methods returning null instead of an empty list; DI misconfiguration leaving the list unset; conditional initialization skipped.","solutions":["Pass Collections.emptyList() instead of null when there is nothing to seed.","Guard the caller: `list = list != null ? list : Collections.emptyList()`.","Fix upstream producers to never return null collections."],"exampleFix":"// before\nnew MinHeap(source.load()); // returns null when empty\n\n// after\nList<HeapElement> elems = source.load();\nnew MinHeap(elems != null ? elems : Collections.emptyList());","handlingStrategy":"validation","validationCode":"List<HeapElement> elems = source != null ? source : Collections.emptyList();\nnew MinHeap(elems);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Pass Collections.emptyList() instead of null when seeding nothing.","Make repository/query methods return empty lists, never null.","Use @Nonnull annotations and static null-analysis."],"tags":["heap","min-heap","null-check","constructor"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}