{"record":{"id":"15f0f53e4bc05c8c","repo":"TheAlgorithms/Java","slug":"cannot-enqueue-null-data","errorCode":null,"errorMessage":"Cannot enqueue null data","messagePattern":"Cannot enqueue null data","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/datastructures/queues/LinkedQueue.java","lineNumber":51,"sourceCode":"\n    /**\n     * Checks if the queue is empty.\n     *\n     * @return true if the queue is empty, otherwise false.\n     */\n    public boolean isEmpty() {\n        return size == 0;\n    }\n\n    /**\n     * Adds an element to the rear of the queue.\n     *\n     * @param data the element to insert.\n     * @throws IllegalArgumentException if data is null.\n     */\n    public void enqueue(T data) {\n        if (data == null) {\n            throw new IllegalArgumentException(\"Cannot enqueue null data\");\n        }\n\n        Node<T> newNode = new Node<>(data);\n\n        if (isEmpty()) {\n            front = newNode;\n        } else {\n            rear.next = newNode;\n        }\n        rear = newNode;\n        size++;\n    }\n\n    /**\n     * Removes and returns the element at the front of the queue.\n     *\n     * @return the element at the front of the queue.\n     * @throws NoSuchElementException if the queue is empty.","sourceCodeStart":33,"sourceCodeEnd":69,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/queues/LinkedQueue.java#L33-L69","documentation":"Thrown by LinkedQueue.enqueue(T) as an IllegalArgumentException when the argument is null. This unbounded linked-list queue explicitly forbids null elements to disambiguate them from the empty-queue sentinel and to keep dequeue()'s return semantics unambiguous.","triggerScenarios":"Passing a literal null to enqueue(), or passing an uninitialized field/map-lookup/Optional.orElse(null) result that evaluates to null. Any code path that fails to initialize a value before enqueuing it triggers the guard.","commonSituations":"Map.get() returning null for a missing key then enqueued; JSON/DB deserialization yielding null fields; default initial values that never got assigned; refactoring that removed an assignment but left the enqueue call.","solutions":["Null-check or use a default before enqueue: if (data != null) queue.enqueue(data).","Filter nulls at the source (e.g. Stream.filter(Objects::nonNull)) before they reach the queue.","Replace missing values with a sentinel/empty representation instead of null."],"exampleFix":"// before\nqueue.enqueue(map.get(key)); // get may return null\n\n// after\nString v = map.get(key);\nif (v != null) {\n    queue.enqueue(v);\n}","handlingStrategy":"validation","validationCode":"if (data != null) {\n    queue.enqueue(data);\n}","typeGuard":"java.util.Objects.nonNull(data)","tryCatchPattern":"null","preventionTips":["Filter nulls at the data source before they reach enqueue.","Use Objects.requireNonNull(data, \"...\") at the boundary to fail with your own message.","Avoid Optional.orElse(null) directly feeding the queue."],"tags":["queue","data-structure","null-check","input-validation","java"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}