{"record":{"id":"c15ab9d3ad3b3837","repo":"TheAlgorithms/Java","slug":"cannot-insert-null-element-c15ab9","errorCode":null,"errorMessage":"Cannot insert null element","messagePattern":"Cannot insert null element","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/datastructures/heaps/MinHeap.java","lineNumber":213,"sourceCode":"     * @return HeapElement with the lowest key\n     * @throws EmptyHeapException if the heap is empty\n     */\n    private HeapElement extractMin() throws EmptyHeapException {\n        if (minHeap.isEmpty()) {\n            throw new EmptyHeapException(\"Cannot extract from empty heap\");\n        }\n        HeapElement result = minHeap.getFirst();\n        deleteElement(1);\n        return result;\n    }\n\n    /**\n     * {@inheritDoc}\n     */\n    @Override\n    public void insertElement(HeapElement element) {\n        if (element == null) {\n            throw new IllegalArgumentException(\"Cannot insert null element\");\n        }\n        minHeap.add(element);\n        toggleUp(minHeap.size());\n    }\n\n    /**\n     * {@inheritDoc}\n     */\n    @Override\n    public void deleteElement(int elementIndex) throws EmptyHeapException {\n        if (minHeap.isEmpty()) {\n            throw new EmptyHeapException(\"Cannot delete from empty heap\");\n        }\n        if ((elementIndex > minHeap.size()) || (elementIndex <= 0)) {\n            throw new IndexOutOfBoundsException(\"Index \" + elementIndex + \" is out of heap range [1, \" + minHeap.size() + \"]\");\n        }\n\n        // Replace with last element and remove last position","sourceCodeStart":195,"sourceCodeEnd":231,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/heaps/MinHeap.java#L195-L231","documentation":"Thrown by MinHeap.insertElement(HeapElement) when the passed element is null. The heap stores HeapElement objects and calls toggleUp which dereferences element.getKey(), so a null would NPE later. The library fails fast with IllegalArgumentException to surface the caller bug at the source.","triggerScenarios":"Passing null to insertElement. Inserting the result of a factory/map lookup that returned null because a key was absent. Inserting from a stream that contains null elements.","commonSituations":"Optional.get() on an empty Optional before insert. Map.get(key) returning null for a missing key, then inserted. Deserialization producing null fields when source data is incomplete.","solutions":["Filter nulls before inserting: `elements.stream().filter(Objects::nonNull).forEach(heap::insertElement)`.","Wrap the insert in a null check at the call site.","Audit upstream data sources that feed the heap to eliminate null production.","Return a sentinel HeapElement instead of null from your factory."],"exampleFix":"// before\nheap.insertElement(map.get(missingKey)); // returns null\n\n// after\nHeapElement e = map.get(key);\nif (e != null) {\n    heap.insertElement(e);\n}","handlingStrategy":"validation","validationCode":"if (element != null) {\n    heap.insertElement(element);\n}","typeGuard":null,"tryCatchPattern":"try {\n    heap.insertElement(element);\n} catch (IllegalArgumentException e) {\n    // element was null — filter upstream instead\n}","preventionTips":["Filter nulls from source collections with Objects::nonNull before insert.","Avoid Optional.orElse(null) feeding the heap.","Enforce non-null contracts at data-source boundaries."],"tags":["heap","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"}