{"record":{"id":"d52fb69b502a7331","repo":"TheAlgorithms/Java","slug":"position","errorCode":null,"errorMessage":"${position}","messagePattern":"\\$\\{position\\}","errorType":"exception","errorClass":"IndexOutOfBoundsException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/datastructures/lists/SinglyLinkedList.java","lineNumber":395,"sourceCode":"    public int getNth(int index) {\n        checkBounds(index, 0, size - 1);\n        SinglyLinkedListNode cur = head;\n        for (int i = 0; i < index; ++i) {\n            cur = cur.next;\n        }\n        return cur.value;\n    }\n\n    /**\n     * @param position to check position\n     * @param low low index\n     * @param high high index\n     * @throws IndexOutOfBoundsException if {@code position} not in range\n     * {@code low} to {@code high}\n     */\n    public void checkBounds(int position, int low, int high) {\n        if (position > high || position < low) {\n            throw new IndexOutOfBoundsException(position + \"\");\n        }\n    }\n\n    /**\n     * Driver Code\n     */\n    public static void main(String[] arg) {\n        SinglyLinkedList list = new SinglyLinkedList();\n        assert list.isEmpty();\n        assert list.size() == 0 && list.count() == 0;\n        assert list.toString().isEmpty();\n\n        /* Test insert function */\n        list.insertHead(5);\n        list.insertHead(7);\n        list.insertHead(10);\n        list.insert(3);\n        list.insertNth(1, 4);","sourceCodeStart":377,"sourceCodeEnd":413,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/lists/SinglyLinkedList.java#L377-L413","documentation":"Thrown by SinglyLinkedList.checkBounds(position, low, high) when position is outside [low, high]. The helper is called by indexed access methods (get, insertAt, etc.) to validate the position; it throws IndexOutOfBoundsException with just the offending position as its message.","triggerScenarios":"Calling get(size) or any indexed method with position == size. Passing a negative position. Position computed from an external size that disagrees with the list's size.","commonSituations":"Off-by-one loop using `<=` against size. Stale position after concurrent modification. Index derived from a parallel array of different length.","solutions":["Validate `position >= low && position <= high` before the indexed call.","Use strict `< list.size()` in iteration bounds.","Recompute the list size before computing positions.","For 0-based indexed access, ensure position is in [0, size-1]."],"exampleFix":"// before\nint v = list.get(i); // i may equal size\n\n// after\nif (i < 0 || i >= list.size()) {\n    throw new IndexOutOfBoundsException(i);\n}\nint v = list.get(i);","handlingStrategy":"validation","validationCode":"if (position >= low && position <= high) {\n    // safe to proceed with indexed access\n}","typeGuard":null,"tryCatchPattern":"try {\n    return list.get(position);\n} catch (IndexOutOfBoundsException e) {\n    // position out of range\n}","preventionTips":["Use strict `< list.size()` in iteration bounds.","Recompute list size before computing positions.","Validate position against [0, size-1] for 0-based access."],"tags":["linked-list","index-out-of-bounds","data-structure","off-by-one","java"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}