{"record":{"id":"229cb4c808fe733e","repo":"TheAlgorithms/Java","slug":"queue-is-empty-cannot-peek-rear","errorCode":null,"errorMessage":"Queue is empty, cannot peek rear","messagePattern":"Queue is empty, cannot peek rear","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/datastructures/queues/Queue.java","lineNumber":101,"sourceCode":"     */\n    @SuppressWarnings(\"unchecked\")\n    public T peekFront() {\n        if (isEmpty()) {\n            throw new IllegalStateException(\"Queue is empty, cannot peek front\");\n        }\n        return (T) queueArray[front];\n    }\n\n    /**\n     * Checks the element at the rear of the queue without removing it.\n     *\n     * @return Element at the rear of the queue.\n     * @throws IllegalStateException if the queue is empty.\n     */\n    @SuppressWarnings(\"unchecked\")\n    public T peekRear() {\n        if (isEmpty()) {\n            throw new IllegalStateException(\"Queue is empty, cannot peek rear\");\n        }\n        return (T) queueArray[rear];\n    }\n\n    /**\n     * Returns true if the queue is empty.\n     *\n     * @return True if the queue is empty.\n     */\n    public boolean isEmpty() {\n        return nItems == 0;\n    }\n\n    /**\n     * Returns true if the queue is full.\n     *\n     * @return True if the queue is full.\n     */","sourceCodeStart":83,"sourceCodeEnd":119,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/queues/Queue.java#L83-L119","documentation":"Thrown by Queue.peekRear() as an IllegalStateException when isEmpty(). The method returns queueArray[rear] without removing it; with nItems == 0 the rear index points at no valid element. Read-only underflow guard, symmetric to peekFront.","triggerScenarios":"Calling peekRear() before any insert(), or after the queue has been fully drained. Reading the rear of an empty ring buffer throws.","commonSituations":"Checking the last-inserted item for batching/coalescing before the producer has sent data; verifying insert order in tests without seeding; logging the tail element on an empty queue.","solutions":["Guard with isEmpty() before peekRear() and return null/Optional when empty.","Ensure at least one insert() precedes the first peekRear().","Catch IllegalStateException when emptiness is an expected branch."],"exampleFix":"// before\nT tail = queue.peekRear(); // throws when empty\n\n// after\nT tail = queue.isEmpty() ? null : queue.peekRear();","handlingStrategy":"validation","validationCode":"T tail = queue.isEmpty() ? null : queue.peekRear();","typeGuard":"null","tryCatchPattern":"null","preventionTips":["isEmpty()-guard peekRear identically to peekFront.","Wrap to Optional<T> so the empty rear is visible in the return type.","Re-check emptiness just before the peek if the producer may have drained."],"tags":["queue","data-structure","underflow","empty-state","java"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}