Skip to content

Any.forEach() throws IndexOutOfBoundsException on empty arrays #188

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
bassner opened this issue May 29, 2018 · 0 comments
Closed

Any.forEach() throws IndexOutOfBoundsException on empty arrays #188

bassner opened this issue May 29, 2018 · 0 comments

Comments

@bassner
Copy link

bassner commented May 29, 2018

If I use the forEach() method of Any on an empty array, a IndexOutOfBoundsException is thrown.

Simple example to reproduce:

Any a = JsonIterator.deserialize("[[\"Foo\",\"Bar\"],[]]");
a.forEach(b -> b.forEach(c -> System.out.println(c.toString())));

This results in:

Foo
Bar
Exception in thread "main" java.lang.IndexOutOfBoundsException
	at com.jsoniter.any.ArrayLazyAny.fillCacheUntil(ArrayLazyAny.java:163)
	at com.jsoniter.any.ArrayLazyAny.access$000(ArrayLazyAny.java:13)
	at com.jsoniter.any.ArrayLazyAny$LazyIterator.<init>(ArrayLazyAny.java:197)
	at com.jsoniter.any.ArrayLazyAny.iterator(ArrayLazyAny.java:79)
	at java.lang.Iterable.forEach(Iterable.java:74)
...

A simple workaround is to get the array as a list and use the forEach() method of the list:

Any a = JsonIterator.deserialize("[[\"Foo\",\"Bar\"],[]]");
a.forEach(b -> b.asList().forEach(c -> System.out.println(c.toString())));

The dependency I used in my pom.xml:

<dependency>
    <groupId>com.jsoniter</groupId>
    <artifactId>jsoniter</artifactId>
    <version>0.9.22</version>
</dependency>

Am I doing something wrong or is this a bug?

Thank you!

@taowen taowen closed this as completed in 301f44b Jul 1, 2018
taowen added a commit that referenced this issue Jul 1, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant