3

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)
n00b1990
  • 1,189
  • 5
  • 17
  • 25
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Oleg Estekhin Jun 17 '14 at 08:04
  • My problem is actually not the null pointer, but the proper setting of an element in this list – n00b1990 Jun 17 '14 at 08:06
  • 1
    Please show the full stack trace. – Jon Skeet Jun 17 '14 at 08:06
  • 1
    what is `ObservableList` and what does `series.getData()` return? [List#set(int, E)](http://docs.oracle.com/javase/8/docs/api/java/util/List.html#set-int-E-) throw a NPE when E is null and this list does not permit null (ofcourse if ObservableList is an impl of List) – A4L Jun 17 '14 at 08:08
  • Not enough information. Need to see Series() and Series.getData() – laune Jun 17 '14 at 08:10
  • `series.getData()` returns an observable list, which extends `java.util.List`. Series is the plot (line) in a JavaFX graph and by invoking `getData()` you get a list back with all points (A list with the present x,y values) – n00b1990 Jun 17 '14 at 08:11
  • The problem does not occur in this snippet because it's in line 657. In the original code, there must be some observers being attached, and when these are alerted due to the change, somehting breaks when dispatching. Post code that REALLY demonstrates the problem. – laune Jun 17 '14 at 08:29
  • I don't understand you. This code IS the problem. All the other code is never executed and I can make a new class, copy this main method in it and still would have the same problem. – n00b1990 Jun 17 '14 at 08:38
  • This snippet is line 657, the other code is just not copied – n00b1990 Jun 17 '14 at 08:45
  • Commenters: [ObservableList](http://docs.oracle.com/javase/8/javafx/api/javafx/collections/ObservableList.html), [Data](http://docs.oracle.com/javase/8/javafx/api/javafx/scene/chart/XYChart.Data.html), and [Series](http://docs.oracle.com/javase/8/javafx/api/javafx/scene/chart/XYChart.Series.html) are parts of the JavaFX API. – Stuart Marks Jun 23 '14 at 05:23

2 Answers2

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.

Stuart Marks
  • 127,867
  • 37
  • 205
  • 259
1

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.

Jonathan Giles
  • 666
  • 4
  • 7