My favorites | Sign in
Project Home Issues
New issue   Search
for
  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
Owner:  e...@google.com
Closed:  Sep 2009
Cc:  b...@google.com, e...@google.com

Restricted
  • Only users with Commit permission may comment.


Sign in to add a comment
 
Reported by zhanghua...@gmail.com, Jul 22, 2008
The following reproduces the bug constantly:
 public void testGetNextSibling() {
  Document document = null;
  try {
   document = DocumentBuilderFactory.newInstance()
     .newDocumentBuilder().parse(
       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
#1 enge...@gmail.com
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
#2 dkde...@gmail.com
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
#3 roland...@gmail.com
This issue still exists in the SDK 1.5
Aug 24, 2009
#4 jlapenna@google.com
(No comment was entered for this change.)
Labels: Component-Framework
Aug 27, 2009
#5 jbq+legacy@google.com
(No comment was entered for this change.)
Labels: -Component-Framework Component-Dalvik
Sep 23, 2009
#6 e...@google.com
 Issue 1863  has been merged into this issue.
Sep 23, 2009
#7 e...@google.com
(the later duplicate had internal issue 1606217 raised to cover it.)
Summary: org.w3c.dom.Node#getNextSibling throws IndexOutOfBoundsException
Status: Assigned
Owner: e...@google.com
Sep 25, 2009
#8 dgknel...@gmail.com
Hit this as well, but seemingly only when my XML contains CDATA.
Sep 25, 2009
#9 e...@google.com
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 e...@google.com
(No comment was entered for this change.)
Owner: e...@google.com
Jun 11, 2010
#11 callinte...@web.de
Hi,

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?

Thanks.
Jun 11, 2010
Project Member #12 e...@google.com
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
#13 sne...@gmail.com
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 e...@google.com
 Issue 8944  has been merged into this issue.
Jul 7, 2010
Project Member #15 e...@google.com
@13: this bug is believed fixed. please supply a reproduceable test case. our regression test is in libcore/luni/src/test/java/tests/xml/NodeTest.java (test_getNextSibling) and still passes.
Jul 7, 2010
#16 sne...@gmail.com
@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=\"http://www.w3.org/2001/XMLSchema\" " +
				 "		  xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" +
				 "<ErrorCode>0</ErrorCode>" +
				 "<Message />" +
				 "<Package xsi:type=\"sessionInfo\">" +
				 "	<sid>3dz2ja04sjxe3fxdyhizv2zr</sid>" +
				 "</Package>" +
				 "</Result>";
	
	DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
	ByteArrayInputStream bs = new ByteArrayInputStream(xml.getBytes( "utf-8" ));
	Document doc = builder.parse(bs);
	assertNotNull(doc);
	
	// Get and test the document element
	Element element = doc.getDocumentElement();
	assertNotNull(element);
	assertEquals("Result", element.getTagName());
	
	element = (Element)element.getFirstChild();
	while (element != null) {
		if (element.getNodeType() != Element.ELEMENT_NODE) {
			element = (Element)element.getNextSibling();
			continue;
		}
		
		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  - https://code.google.com/p/android/issues/detail?id=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(ArrayList.java:341)
at org.apache.harmony.xml.dom.InnerNodeImpl.getNextSibling(InnerNodeImpl.java:76)
at com.mediamonkey.app.test.XmlParserTests.testXml_getNextSibling(XmlParserTests.java:70)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:430)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1447)
Jul 7, 2010
Project Member #17 e...@google.com
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
#18 sne...@gmail.com
@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 e...@google.com
@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 e...@google.com
(No comment was entered for this change.)
Labels: Restrict-AddIssueComment-Commit
Sign in to add a comment

Powered by Google Project Hosting