{"record":{"id":"c9623253e02786c3","repo":"jhy/jsoup","slug":"no-child-at-index","errorCode":null,"errorMessage":"No child at index: ","messagePattern":"No child at index: ","errorType":"exception","errorClass":"IndexOutOfBoundsException","httpStatus":null,"severity":"error","filePath":"src/main/java/org/jsoup/nodes/Element.java","lineNumber":368,"sourceCode":"     * </p>\n     *\n     * @param index the index number of the element to retrieve\n     * @return the child element, if it exists, otherwise throws an {@code IndexOutOfBoundsException}\n     * @see #childNode(int)\n     */\n    public Element child(int index) {\n        Validate.isTrue(index >= 0, \"Index must be >= 0\");\n        List<Element> cached = cachedChildren();\n        if (cached != null) return cached.get(index);\n        // otherwise, iter on elementChild; saves creating list\n        int size = childNodes.size();\n        for (int i = 0, e = 0; i < size; i++) { // direct iter is faster than chasing firstElSib, nextElSibd\n            Node node = childNodes.get(i);\n            if (node instanceof Element) {\n                if (e++ == index) return (Element) node;\n            }\n        }\n        throw new IndexOutOfBoundsException(\"No child at index: \" + index);\n    }\n\n    /**\n     * Get the number of child nodes of this element that are elements.\n     * <p>\n     * This method works on the same filtered list like {@link #child(int)}. Use {@link #childNodes()} and {@link\n     * #childNodeSize()} to get the unfiltered Nodes (e.g. includes TextNodes etc.)\n     * </p>\n     *\n     * @return the number of child nodes that are elements\n     * @see #children()\n     * @see #child(int)\n     */\n    public int childrenSize() {\n        if (childNodeSize() == 0) return 0;\n        return childElementsList().size(); // gets children into cache; faster subsequent child(i) if unmodified\n    }\n","sourceCodeStart":350,"sourceCodeEnd":386,"githubUrl":"https://github.com/jhy/jsoup/blob/9851ac5d9c576c6888910b5a51a2362bbc978959/src/main/java/org/jsoup/nodes/Element.java#L350-L386","documentation":"Element.child(int index) indexes only child nodes that are themselves Elements (skipping text/comment nodes). If the element has fewer element children than the requested index, jsoup throws IndexOutOfBoundsException with 'No child at index: N'.","triggerScenarios":"Calling element.child(i) where i >= element.childrenSize(); e.g. element.child(0) on an element containing only text nodes, or child(5) on a table whose 5th child is a text node.","commonSituations":"HTML where whitespace/text nodes interleave with elements and developers assume child indexes match childNodes() indexes; empty or unexpectedly structured documents after a site layout change.","solutions":["Check element.childrenSize() (or children().size()) before calling child(index)","Use children().get(index) with a bounds check, or select(...) with a CSS selector to target the element directly","If you need raw child nodes including text, use childNode(index) / childNodes() instead","Verify the parsed document actually contains the expected structure (log element.children() during debugging)"],"exampleFix":"// before\nElement first = doc.body().child(0); // throws if body has only text\n// after\nElements kids = doc.body().children();\nElement first = kids.isEmpty() ? null : kids.get(0);","handlingStrategy":"validation","validationCode":"Element safeChild(Element parent, int index) { return index >= 0 && index < parent.childrenSize() ? parent.child(index) : null; }","typeGuard":null,"tryCatchPattern":"try { Element e = parent.child(i); } catch (IndexOutOfBoundsException e) { Element e = null; /* handle missing child */ }","preventionTips":["Check childrenSize() before indexing","Remember child(int) skips non-element nodes; use childNode(int) for raw nodes","Prefer select() with CSS selectors for structural lookups","Log children() when parsing unexpected markup"],"tags":["index-out-of-bounds","dom","element"],"backgroundTag":"index-out-of-bounds","analyzedSha":"9851ac5d9c576c6888910b5a51a2362bbc978959","analyzedAt":"2026-09-08T15:22:04.931Z","contentChangedAt":"2026-09-08T15:22:04.931Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}