Re: [Bug] NullPointerException in TreeList$TreeListIterator.previous() when initialized with invalid fromIndex

Gary Gregory <[email protected]> Thu, 15 Jan 2026 12:38:28 -0500
Newsgroups gmane.comp.jakarta.commons.user
Message-ID <CACZkXPz2wQ6xQWny+dcvO3zEh1batHMWk288Lp9Bnj4mtcKXBQ@mail.gmail.com>
Hello [email protected],

This is not a bug. The TreeListIterator class is package-private and
can only be built in client code from the public method
TreeList.listIterator(int), which performs argument checks and throws
IndexOutOfBoundsException.

Can you make this happen from client code? IOW, code that doesn't
reside in the Commons Collection namespace?

Please see the new test
org.apache.commons.collections4.list.TreeListTest.testTreeListIteratorConst=
ruction()
in git master. Nevertheless, I've moved the argument checking into the
constructor for clarity.

Was this AI-generated? Fuzzed?

Thank you,
Gary

On Thu, Jan 15, 2026 at 11:58=E2=80=AFAM A A <[email protected]> wrote:
>
> *Version:*
> commons-collections-4.5.0
>
> *Description:*
>
> In org.apache.commons.collections4.list.TreeList$TreeListIterator.previou=
s(
> TreeList.java:863).
>
> The next reference is not checked for null before calling getValue(), whi=
ch
> can result in a NullPointerException.
>
> ```java
> @Override
> public E previous() {
> checkModCount();
> if (!hasPrevious()) {
> throw new NoSuchElementException("Already at start of list.");
> }
> if (next =3D=3D null) {
> next =3D parent.root.get(nextIndex - 1);
> } else {
> next =3D next.previous();
> }
> final E value =3D next.getValue(); // NPE !
> current =3D next;
> currentIndex =3D --nextIndex;
> return value;
> }
> ```
>
> *Step to reproduce:*
>
> Test Code:
> ```java
> package org.apache.commons.collections4.list;
> import org.junit.Test;
> import org.apache.commons.collections4.list.TreeList;
> import java.time.format.TextStyle;
> import java.util.NoSuchElementException;
>
> public class TestClass {
> @Test(timeout=3D3000)
> public void test() throws Throwable {
> TreeList<TextStyle> treeList0 =3D new TreeList<TextStyle>();
> treeList0.add(TextStyle.FULL);
> treeList0.add(TextStyle.SHORT);
> TreeList.TreeListIterator<TextStyle> treeList_TreeListIterator0 =3D new
> TreeList.TreeListIterator<TextStyle>(treeList0, 3);
> treeList_TreeListIterator0.previous();
>
> }
> }
> ```
>
> Execution Result:
> ```
> JUnit version 4.12
> .E
> Time: 0.016
> There was 1 failure:
> 1) test(org.apache.commons.collections4.list.TestClass)
> java.lang.NullPointerException
> at org.apache.commons.collections4.list.TreeList$TreeListIterator.previou=
s(
> TreeList.java:863)
> at org.apache.commons.collections4.list.TestClass.test(TestClass.java:15)
>
> FAILURES!!!
> Tests run: 1, Failures: 1
> ```
>
> Environment:
> ```
> Apache Maven 3.9.1
> Java version: 1.8.0_452, vendor: Private Build, runtime:
> /usr/lib/jvm/java-8-openjdk-amd64/jre
> Default locale: en, platform encoding: UTF-8
> OS name: "linux", version: "5.15.0-138-generic", arch: "amd64", family:
> "unix"
> ```