FilteredList在更新时提供java.lang.ArrayIndexOutOfBoundsException [英] FilteredList gives java.lang.ArrayIndexOutOfBoundsException on update

查看:239
本文介绍了FilteredList在更新时提供java.lang.ArrayIndexOutOfBoundsException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个简单的应用程序来测试过滤列表及其相应源列表更改时的行为。我也想测试更新更改,所以我创建了 ObservableList ObservableList s 。它比创建具有可观察字段的其他类更快更简单。

I created a simple application to test filtered lists and their behavior when the corresponding source list changes. I'd like to test update changes also, so I created ObservableList of ObservableLists. It is faster and simpler than creating additional class like Person that have observable fields.

代码看起来如此:

    ListChangeListener<ObservableList<String>> changeNotifier = new ListChangeListener<ObservableList<String>>() {
        @Override
        public void onChanged(Change<? extends ObservableList<String>> c) {
            while (c.next()) {
                if (c.wasPermutated()) {
                    System.out.println("permutation");
                } else if (c.wasUpdated()) {
                    System.out.println("update");
                } else {
                    if (c.wasRemoved()) {
                        System.out.println("remove");
                    }
                    if (c.wasAdded()) {
                        System.out.println("add");
                    }
                    if (c.wasReplaced()) {
                        System.out.println("replace");
                    }
                }
            }
        }
    };
    Callback<ObservableList<String>, Observable[]> identityExtractor = new Callback<ObservableList<String>, Observable[]>() {
        @Override
        public Observable[] call(ObservableList<String> param) {
            return new Observable[]{param};
        }
    };
    Predicate<ObservableList<String>> nonEmptyFilter = new Predicate<ObservableList<String>>() {
        @Override
        public boolean test(ObservableList<String> obsl) {
            boolean nonEmpty = ! obsl.isEmpty();
            for (String item : obsl) {
                nonEmpty = nonEmpty && (null != item) && ("" != item);
            };
            return nonEmpty;
        }
    };

    ObservableList<ObservableList<String>> basicSimple = FXCollections.observableArrayList();
    ObservableList<ObservableList<String>> basicComposed = FXCollections.observableArrayList( identityExtractor );

    ObservableList<ObservableList<String>> filteredSimple = basicSimple.filtered( nonEmptyFilter );
    ObservableList<ObservableList<String>> filteredComposed = basicComposed.filtered( nonEmptyFilter );

    System.out.println("Basic testing");

    System.out.println("Add invalid");
    basicSimple.addAll( FXCollections.observableArrayList("") );
    System.out.println( basicSimple );
    System.out.println( filteredSimple );

    System.out.println("Make it valid");
    basicSimple.get(0).addAll("first");
    System.out.println( filteredSimple );

    System.out.println("Add valid");
    basicSimple.addAll( FXCollections.observableArrayList("Second") );
    System.out.println( filteredSimple );

    System.out.println("Composed testing");

    System.out.println("Add invalid");
    basicComposed.addAll( FXCollections.observableArrayList("") );
    System.out.println( basicComposed );
    System.out.println( filteredComposed );

    System.out.println("Make it valid");
    basicComposed.get(0).addAll("first");
    System.out.println( filteredComposed );

    System.out.println("Add valid");
    basicComposed.addAll( FXCollections.observableArrayList("Second") );
    System.out.println( filteredComposed );

我在测试过程中发现了一个奇怪的错误:

I've discovered a strange error during testing:

[info] Running helloworld.HelloWorld 
Basic testing
Add invalid
[[]]
[]
Make it valid
[]
Add valid
[[Second]]
Composed testing
Add invalid
[[]]
[]
Make it valid
[error] (JavaFX Application Thread) java.lang.ArrayIndexOutOfBoundsException
java.lang.ArrayIndexOutOfBoundsException
        at java.lang.System.arraycopy(Native Method)
        at javafx.collections.transformation.FilteredList.updateFilter(FilteredList.java:298)
        at javafx.collections.transformation.FilteredList.update(FilteredList.java:239)
        at javafx.collections.transformation.FilteredList.sourceChanged(FilteredList.java:137)
        at javafx.collections.transformation.TransformationList.lambda$getListener$16(TransformationList.java:106)
        at javafx.collections.transformation.TransformationList$$Lambda$63/1596532574.onChanged(Unknown Source)
        at javafx.collections.WeakListChangeListener.onChanged(WeakListChangeListener.java:88)
        at com.sun.javafx.collections.ListListenerHelper$SingleChange.fireValueChangedEvent(ListListenerHelper.java:164)
        at com.sun.javafx.collections.ListListenerHelper.fireValueChangedEvent(ListListenerHelper.java:73)
        at javafx.collections.ObservableListBase.fireChange(ObservableListBase.java:233)
        at javafx.collections.ListChangeBuilder.commit(ListChangeBuilder.java:485)
        at javafx.collections.ListChangeBuilder.endChange(ListChangeBuilder.java:541)
        at javafx.collections.ObservableListBase.endChange(ObservableListBase.java:205)
        at com.sun.javafx.collections.ObservableListWrapper.access$200(ObservableListWrapper.java:45)
        at com.sun.javafx.collections.ObservableListWrapper$1$1.invalidated(ObservableListWrapper.java:75)
        at com.sun.javafx.collections.ListListenerHelper$SingleInvalidation.fireValueChangedEvent(ListListenerHelper.java:126)
        at com.sun.javafx.collections.ListListenerHelper.fireValueChangedEvent(ListListenerHelper.java:73)
        at javafx.collections.ObservableListBase.fireChange(ObservableListBase.java:233)
        at javafx.collections.ListChangeBuilder.commit(ListChangeBuilder.java:482)
        at javafx.collections.ListChangeBuilder.endChange(ListChangeBuilder.java:541)
        at javafx.collections.ObservableListBase.endChange(ObservableListBase.java:205)
        at javafx.collections.ModifiableObservableListBase.addAll(ModifiableObservableListBase.java:102)
        at javafx.collections.ObservableListBase.addAll(ObservableListBase.java:245)
        at helloworld.HelloWorld.start(HelloWorld.java:87)
        at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$153(LauncherImpl.java:821)
        at com.sun.javafx.application.LauncherImpl$$Lambda$55/7143454.run(Unknown Source)
        at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$166(PlatformImpl.java:323)
        at com.sun.javafx.application.PlatformImpl$$Lambda$51/397137382.run(Unknown Source)
        at com.sun.javafx.application.PlatformImpl.lambda$null$164(PlatformImpl.java:292)
        at com.sun.javafx.application.PlatformImpl$$Lambda$53/1802784360.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at com.sun.javafx.application.PlatformImpl.lambda$runLater$165(PlatformImpl.java:291)
        at com.sun.javafx.application.PlatformImpl$$Lambda$52/1184782272.run(Unknown Source)
        at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
        at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
        at com.sun.glass.ui.gtk.GtkApplication.lambda$null$45(GtkApplication.java:126)
        at com.sun.glass.ui.gtk.GtkApplication$$Lambda$43/450111611.run(Unknown Source)
        at java.lang.Thread.run(Thread.java:745)
[trace] Stack trace suppressed: run last compile:run for the full output.
[]
Add valid
[[Second]]

basicSimple basicComposed 之间的区别是后者定义了一个提取器,因此它接收更新事件。在处理更新事件的过程中,本机代码抛出异常。我应该考虑如何使示例代码正常工作?

The difference between basicSimple and basicComposed is that latter have an extractor defined, so it receives update events. In the middle of processing update event the exception was thrown by a native code. What should I consider to make the sample code work without errors?

我已经将println插入到 nonEmptyFilter 谓词测试的末尾。它正常工作,并传递给它的ObservableList是刚刚更新的预期新值。当FilteredList的某些iternal代码正在执行时,会发生错误。

I've inserted println to the end of the nonEmptyFilter predicate test. It works correctly and the ObservableList passed to it is as expected new value that was just updated. The error occurs later when some iternal code of FilteredList is executing.

推荐答案

这看起来像一个bug;我认为它可能与这一个相同,错误报告中说的是已修复为JavaFX 8u60(可能对8u40来说太晚了)。

This looks like a bug; I think it is probably the same as this one, which the bug report says is fixed for JavaFX 8u60 (presumably too late for 8u40).

我在1.8.0_40-ea-b23上测试过?和1.9.0-ea-b49两个版本都出现了错误。如果目前有一个1.8.0u60的ea版本,我不知道在哪里找到它。

I tested on 1.8.0_40-ea-b23?? and 1.9.0-ea-b49 and the error appeared in both versions. If there is currently an ea release of 1.8.0u60 I'm not sure where to find it.

这篇关于FilteredList在更新时提供java.lang.ArrayIndexOutOfBoundsException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆