My favorites | Sign in
Project Home Issues
New issue   Search
  Advanced search   Search tips   Subscriptions
Issue 779: org.w3c.dom.Node#getNextSibling throws IndexOutOfBoundsException
13 people starred this issue and may be notified of changes. Back to list
Status:  Released
Closed:  Sep 2009

  • Only users with Commit permission may comment.

Sign in to add a comment
Reported by, Jul 22, 2008
The following reproduces the bug constantly:
 public void testGetNextSibling() {
  Document document = null;
  try {
   document = DocumentBuilderFactory.newInstance()
       new ByteArrayInputStream("<root/>".getBytes()));
  } catch (Exception e) {
   throw new Error(e);
  Node root = document.getDocumentElement();
  root.getNextSibling(); // should return null here.

Would you please confirm? Thanks!
Aug 7, 2008
I also have met this problem. But now I hava solved it. That's because of the end of
xml file must be a CRLF. If have not, an IndexOutOfBoundsExceptin error will be throwed.
Jan 13, 2009
Hit this as well, CRLF didn't help.

Using dumb workaround:

while (node != null) {
  [...actual code...]
  try {
    node = node.getNextSibling();
  catch (IndexOutOfBoundsException e) {
    node = null;
Jun 18, 2009
This issue still exists in the SDK 1.5
Aug 24, 2009
(No comment was entered for this change.)
Labels: Component-Framework
Aug 27, 2009
(No comment was entered for this change.)
Labels: -Component-Framework Component-Dalvik
Sep 23, 2009
 Issue 1863  has been merged into this issue.
Sep 23, 2009
(the later duplicate had internal issue 1606217 raised to cover it.)
Summary: org.w3c.dom.Node#getNextSibling throws IndexOutOfBoundsException
Status: Assigned
Sep 25, 2009
Hit this as well, but seemingly only when my XML contains CDATA.
Sep 25, 2009
fixed internally. the problem was an incorrect bounds check:

     public Node getNextSibling() {
-        if (parent == null || index >= parent.children.size()) {
+        if (parent == null || index + 1 >= parent.children.size()) {
             return null;

         return parent.children.get(index + 1);

dkdecha's work-around should give correct results in the meantime.
Status: FutureRelease
May 15, 2010
Project Member #10
(No comment was entered for this change.)
Jun 11, 2010

pretty old, what means "FutureRelease"? I had to waste some time as well since the documentation says it returns null. Can you please either fix it in the near future or at least put into the documentation how it works?

Jun 11, 2010
Project Member #12
judging by eclair's LeafNodeImpl, this is fixed in eclair (2.1, that is; i'm pretty sure it was too late for 2.0).
Status: Released
Labels: Target-Eclair
Jul 5, 2010
Hm, this is odd, I get this behavior running on 2.1 update1. Did you guys miss to fix this or is the "throws an Exception" the "proper way" for Android?
Jul 7, 2010
Project Member #14
 Issue 8944  has been merged into this issue.
Jul 7, 2010
Project Member #15
@13: this bug is believed fixed. please supply a reproduceable test case. our regression test is in libcore/luni/src/test/java/tests/xml/ (test_getNextSibling) and still passes.
Jul 7, 2010
@15: This test case will fail with a IndexOutOfBoundsException
Please let me know if you need any additional info.

public void testXml_getNextSibling() throws Exception {
	String xml = "<Result xmlns:xsd=\"\" " +
				 "		  xmlns:xsi=\"\">" +
				 "<ErrorCode>0</ErrorCode>" +
				 "<Message />" +
				 "<Package xsi:type=\"sessionInfo\">" +
				 "	<sid>3dz2ja04sjxe3fxdyhizv2zr</sid>" +
				 "</Package>" +
	DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
	ByteArrayInputStream bs = new ByteArrayInputStream(xml.getBytes( "utf-8" ));
	Document doc = builder.parse(bs);
	// Get and test the document element
	Element element = doc.getDocumentElement();
	assertEquals("Result", element.getTagName());
	element = (Element)element.getFirstChild();
	while (element != null) {
		if (element.getNodeType() != Element.ELEMENT_NODE) {
			element = (Element)element.getNextSibling();
		if (element.getTagName().equals( "ErrorCode" )) {
			assertEquals(0, Integer.parseInt(element.getChildNodes().item(0).getNodeValue()));
		} else if (element.getTagName().equals("Message")) {
			if (element.getChildNodes().getLength() > 0) {
				String str = element.getChildNodes().item(0).getNodeValue();
				assertTrue(str == null || str.length() < 1);
		// Will throw exception on last node
		// See:  Issue 779  -
		element = (Element)element.getNextSibling();
	assertNull("element should be null", element);

Copy of stack trace from test run:
java.lang.IndexOutOfBoundsException: Invalid location 3, size is 3
at java.util.ArrayList.get(
at org.apache.harmony.xml.dom.InnerNodeImpl.getNextSibling(
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.AndroidTestRunner.runTest(
at android.test.AndroidTestRunner.runTest(
at android.test.InstrumentationTestRunner.onStart(
Jul 7, 2010
Project Member #17
that test passes for me on a 2.2 device and the 2.2 emulator. you did remember to create a 2.2 AVD for your emulator? you're not accidentally running an old AVD?
Jul 7, 2010
@17: Well, as I mentioned, this occurs in Android 2.1 update 1. I was under the impression that it should work as expected in 2.1. See my initial comment (#13).

I'll test it with a 2.2 AVD later on, it's the middle of the night here in Sweden.
Jul 7, 2010
Project Member #19
@18: sorry; i've spent the day looking at open source bugs and got mixed up about which version you were having trouble with. okay, i retested on 2.1-1, and you're right: this fix didn't make it in. i've checked the source tree too, and it agrees. (there was a lot of confusion back then about what various releases would be called, and what fixes were going where.) but 2.2 _does_ have the fix. i've updated this bug's labels accordingly. thanks for pointing this out!
Labels: -Target-Eclair Target-Froyo
Dec 3, 2012
Project Member #20
(No comment was entered for this change.)
Labels: Restrict-AddIssueComment-Commit
Sign in to add a comment

Powered by Google Project Hosting