You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Haoran Yan <un...@gmail.com> on 2026/01/16 04:00:37 UTC

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

Thanks for clarifying, Gary. I’ll take a look.


发件人: Gary Gregory <ga...@gmail.com>
日期: 星期五, 2026年1月16日 01:38
收件人: Commons Users List <us...@commons.apache.org>
主题: Re: [Bug] NullPointerException in TreeList$TreeListIterator.previous() when initialized with invalid fromIndex
Hello unzhgppt@gmail.com,

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.testTreeListIteratorConstruction()
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 AM A A <un...@gmail.com> wrote:
>
> *Version:*
> commons-collections-4.5.0
>
> *Description:*
>
> In org.apache.commons.collections4.list.TreeList$TreeListIterator.previous(
> TreeList.java:863).
>
> The next reference is not checked for null before calling getValue(), which
> can result in a NullPointerException.
>
> ```java
> @Override
> public E previous() {
> checkModCount();
> if (!hasPrevious()) {
> throw new NoSuchElementException("Already at start of list.");
> }
> if (next == null) {
> next = parent.root.get(nextIndex - 1);
> } else {
> next = next.previous();
> }
> final E value = next.getValue(); // NPE !
> current = next;
> currentIndex = --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=3000)
> public void test() throws Throwable {
> TreeList<TextStyle> treeList0 = new TreeList<TextStyle>();
> treeList0.add(TextStyle.FULL);
> treeList0.add(TextStyle.SHORT);
> TreeList.TreeListIterator<TextStyle> treeList_TreeListIterator0 = 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.previous(
> 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"
> ```

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org