Skip to Content.
Sympa Menu

xom-interest - [XOM-interest] getChild sometimes returns null and sometimes throws IndexOutOfBoundsException

xom-interest AT lists.ibiblio.org

Subject: XOM API for Processing XML with Java

List archive

Chronological Thread  
  • From: Michael Whapples <software AT mdw.onl>
  • To: XOM API for Processing XML with Java <xom-interest AT lists.ibiblio.org>
  • Subject: [XOM-interest] getChild sometimes returns null and sometimes throws IndexOutOfBoundsException
  • Date: Tue, 14 Mar 2017 13:59:43 +0000

Hello,

I have found if my code tries to call getChild with an index greater than the number of child nodes, sometimes it will return null and other times it will throw an IndexOutOfBoundsException. This does not seem to be entirely random and seems to be reproduceable. When I tested it, getChild(1) called when the element only has one child throws the exception. Similar for getChild(2) called on an element with 2 child nodes. However for getChild(3) called on an element with 3 child nodes will return null.


Is there any reason why XOM does different things for what seems to be the same problem?


Below is a Java class demonstrating the issue.


import nu.xom.Element;

public class App {
public static void main(String[] args) {
Element e1 = new Element("p");
Element c1 = new Element("s");
e1.appendChild(c1);
Element c2 = new Element("b");
e1.appendChild(c2);
Element c3 = new Element("s");
e1.appendChild(c3);
System.out.println(e1.getChild(3));
Element e2 = new Element("p");
Element cn1 = new Element("s");
e2.appendChild(cn1);
System.out.println(e2.getChild(1));
}
}

Regards


Michael Whapples





Archive powered by MHonArc 2.6.24.

Top of Page