1

I'm making an XY line chart, and am getting and Unchecked call warning when I add a data point

XYChart.Series series = new XYChart.Series();
series.setName("Temp Res graph");
for(int i = 1; i<800; i++) {
        XYChart.Data dp = new XChart.Data(i,Integer.parseInt(getTemp(i)));
        series.getData().add(dp);
        lineChart.getData().add(series);
}

Its a warning, but when I run the code I get a bunch of errors, that seem to be caused by those lines:


Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: Duplicate series added
Sanjit Sarda
  • 381
  • 1
  • 4
  • 13

1 Answers1

4

The unchecked warning is from your use of raw types. Both XYChart.Series and XYChart.Data are generic types but you don't specify any generic parameters. Based on the values you're passing to the Data constructor you should be using Number for both parameters.

As for your error, you're adding series to your lineChart at the end of every iteration of your for loop. Move that code outside the loop (either before or after).

// Add generic parameters (uses the <> operator on the right)
XYChart.Series<Number, Number> series = new XYChart.Series<>();
series.setName("Temp Res graph");
for(int i = 1; i<800; i++) {
    // Add generic parameters (uses the <> operator on the right)
    XYChart.Data<Number, Number> dp = new XChart.Data<>(i,Integer.parseInt(getTemp(i)));
    series.getData().add(dp);
}
lineChart.getData().add(series); // outside loop

Note that LineChart is also a generic type; don't forget to specify the generic parameters for it as well: LineChart<Number, Number>.

Slaw
  • 37,820
  • 8
  • 53
  • 80