I get an exception when trying to swap elements in an observable list?

倾然丶 夕夏残阳落幕 提交于 2019-12-12 17:14:56

问题


I have the following code, which seems very simple and straight-forward:

public static void main(String[] args) {
    Series<String, Number> series = new Series<String, Number>();
    ObservableList<Data<String, Number>> list = series.getData();

    list.add(new Data<String,Number>("1", new Double(1)));
    list.add(new Data<String,Number>("2", new Double(2)));
    list.add(new Data<String,Number>("3", new Double(3)));
    list.add(new Data<String,Number>("4", new Double(4)));
    int size = list.size();
    for (int i = 0; i < size-1; i++) {
        list.set(i, list.get(i+1));
    }
    list.remove(size-1);
}

The problem is that I get a null pointer exception at the line of setting the list. Shouldn't this code do what it's supposed to do? I check the size of the list and that returns 4. The only thing I can think of is that I'm missing something here and don't know how to properly set an element at the specified index?

Can you help me?

EDIT: Stacktrace:

Exception in thread "main" java.lang.NullPointerException
    at javafx.scene.chart.XYChart$Series$1.onChanged(Unknown Source)
    at com.sun.javafx.collections.ListListenerHelper$SingleChange.fireValueChangedEvent(Unknown Source)
    at com.sun.javafx.collections.ListListenerHelper.fireValueChangedEvent(Unknown Source)
    at javafx.collections.ObservableListBase.fireChange(Unknown Source)
    at javafx.collections.ListChangeBuilder.commit(Unknown Source)
    at javafx.collections.ListChangeBuilder.endChange(Unknown Source)
    at javafx.collections.ObservableListBase.endChange(Unknown Source)
    at javafx.collections.ModifiableObservableListBase.set(Unknown Source)
    at mypackage.Controller.main(Controller.java:657)

回答1:


Hm, interesting. This reproduces easily on JavaFX 8. In fact the crash still occurs even with this simplified code:

public static void main(String[] args) {
    Series<String, Number> series = new Series<>();
    ObservableList<Data<String, Number>> list = series.getData();
    list.add(new Data<>("1", 1.0));
    list.set(0, new Data<>("2", 2.0));
}

As near as I can tell, the XYChart tries to keep an internal linked list in synch with changes to the ObservableList and there is a bug in this code. The only workaround I can suggest is to copy out the data into a plain list, modify it, and then create a new Series with the modified data. Then remove the old Series and add the new Series to the chart.

A couple asides: it's unnecessary to call new Double(1); you can simply use a double literal 1.0 (as I've done above) since it will get boxed into a Double because a Number is expected in that argument position. Also, the example simply removes the first element from the list, which can be accomplished by replacing the entire loop with list.remove(0). But other changes such as simply setting a new value seem to cause NPE, so this does look like a bug.




回答2:


This issue (in particular the stacktrace) looks like it resembles https://javafx-jira.kenai.com/browse/RT-35831 which has been fixed in JavaFX 8u20. I have 8u20 on my machine and tested the code given in Stuart's comment, and cannot see the exception either.



来源:https://stackoverflow.com/questions/24258683/i-get-an-exception-when-trying-to-swap-elements-in-an-observable-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!