{"record":{"id":"58442c476c01dc83","repo":"TheAlgorithms/Java","slug":"cannot-insert-null-element","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/MaxHeap.java","lineNumber":190,"sourceCode":"     * @return HeapElement with the highest key\n     * @throws EmptyHeapException if the heap is empty\n     */\n    private HeapElement extractMax() throws EmptyHeapException {\n        if (maxHeap.isEmpty()) {\n            throw new EmptyHeapException(\"Cannot extract from an empty heap\");\n        }\n        HeapElement result = maxHeap.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        maxHeap.add(element);\n        toggleUp(maxHeap.size());\n    }\n\n    /**\n     * {@inheritDoc}\n     */\n    @Override\n    public void deleteElement(int elementIndex) throws EmptyHeapException {\n        if (maxHeap.isEmpty()) {\n            throw new EmptyHeapException(\"Cannot delete from an empty heap\");\n        }\n        if ((elementIndex > maxHeap.size()) || (elementIndex <= 0)) {\n            throw new IndexOutOfBoundsException(\"Index \" + elementIndex + \" is out of heap range [1, \" + maxHeap.size() + \"]\");\n        }\n\n        // Replace with last element and remove last position","sourceCodeStart":172,"sourceCodeEnd":208,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/heaps/MaxHeap.java#L172-L208","documentation":"Thrown by MaxHeap.insertElement(HeapElement) when the element is null. Heap ordering relies on HeapElement.getKey() during toggleUp, which would NPE on null; the explicit guard gives a clear message. Null elements are not silently skipped by insert (unlike in the constructor).","triggerScenarios":"Calling insertElement(null); passing a HeapElement from a lookup that returned null; reading elements from a stream/source that yields null slots.","commonSituations":"Optional-empty lookups forwarded directly; deserialization producing null; conditional construction that leaves the element null.","solutions":["Null-check the element before calling insertElement and skip or substitute.","Fix the upstream producer to never yield null HeapElements.","Filter nulls out of any bulk-insert flow before iterating into insertElement."],"exampleFix":"// before\nheap.insertElement(element);\n\n// after\nif (element != null) {\n    heap.insertElement(element);\n}","handlingStrategy":"type-guard","validationCode":"if (element != null) {\n    heap.insertElement(element);\n}","typeGuard":"// No compile-time nullable guard in Java; use explicit null check or Optional.\nOptional.ofNullable(element).ifPresent(heap::insertElement);","tryCatchPattern":null,"preventionTips":["Null-check elements from Optional/Map.get sources before inserting.","Filter nulls in bulk-load paths before iterating into insertElement.","Annotate HeapElement parameters @Nonnull and run static analysis."],"tags":["heap","max-heap","null-check","insert"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}