changeset 6446:a4ee872a75bb

RT-35831 XYChart Series: NullPointerException when changing Series which is not assigned to a chart Reviewed by: jgiles
author Martin Sladecek <martin.sladecek@oracle.com>
date Tue, 11 Mar 2014 16:14:08 +0100
parents b7dbbad9c9bd
children 685651bc38c2
files modules/controls/src/main/java/javafx/scene/chart/XYChart.java modules/controls/src/test/java/javafx/scene/chart/XYChartTest.java
diffstat 2 files changed, 26 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/modules/controls/src/main/java/javafx/scene/chart/XYChart.java	Tue Mar 11 15:48:46 2014 +0100
+++ b/modules/controls/src/main/java/javafx/scene/chart/XYChart.java	Tue Mar 11 16:14:08 2014 +0100
@@ -1477,6 +1477,7 @@
 
         private final ListChangeListener<Data<X,Y>> dataChangeListener = new ListChangeListener<Data<X, Y>>() {
             @Override public void onChanged(Change<? extends Data<X, Y>> c) {
+                final XYChart<X, Y> chart = getChart();
                 while (c.next()) {
                     // RT-25187 Probably a sort happened, just reorder the pointers and return.
                     if (c.wasPermutated()) {
@@ -1504,7 +1505,7 @@
                     if (c.getAddedSize() > 0) {
                         for (Data<X,Y> itemPtr = begin; itemPtr != null; itemPtr = itemPtr.next) {
                             if (itemPtr.setToRemove) {
-                                getChart().dataBeingRemovedIsAdded(itemPtr, Series.this);
+                                if (chart != null) chart.dataBeingRemovedIsAdded(itemPtr, Series.this);
                                 itemPtr.setToRemove = false;
                             }
                         }
@@ -1542,7 +1543,6 @@
                         }
                     }
                     // inform chart
-                    XYChart<X,Y> chart = getChart();
                     if(chart!=null) chart.dataItemsChanged(Series.this,
                             (List<Data<X,Y>>)c.getRemoved(), c.getFrom(), c.getTo(), c.wasPermutated());
                 }
--- a/modules/controls/src/test/java/javafx/scene/chart/XYChartTest.java	Tue Mar 11 15:48:46 2014 +0100
+++ b/modules/controls/src/test/java/javafx/scene/chart/XYChartTest.java	Tue Mar 11 16:14:08 2014 +0100
@@ -43,6 +43,7 @@
 import javafx.stage.Stage;
 import org.junit.Assert;
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertSame;
 
 
 public class XYChartTest extends ChartTestBase {
@@ -134,4 +135,27 @@
         assertEquals(0, areachart.getLegend().prefHeight(-1), 0);            
         assertEquals(0, areachart.getLegend().prefWidth(-1), 0);            
     }
+
+
+    @Test public void canModifySeriesWithoutChart() {
+        XYChart.Series series = new XYChart.Series();
+
+        ObservableList<XYChart.Data> dataList1 = FXCollections.observableArrayList();
+        dataList1.add(new XYChart.Data(0, 1));
+        dataList1.add(new XYChart.Data(1, 2));
+        dataList1.add(new XYChart.Data(2, 3));
+
+        series.setData(dataList1);
+
+        assertSame(dataList1, series.getData());
+
+        ObservableList<XYChart.Data> dataList2 = FXCollections.observableArrayList();
+        dataList2.add(new XYChart.Data(0, 3));
+        dataList2.add(new XYChart.Data(1, 2));
+        dataList2.add(new XYChart.Data(2, 1));
+
+        series.setData(dataList2);
+
+        assertSame(dataList2, series.getData());
+    }
 }