0

I am studying JavaFX and I am also a newbie in Java. (I am using Java8 in Windows.)

Sample code is like below.

XYChart.Series series1 = new XYChart.Series();
eries1.setName("남자");
series1.setData(FXCollections.observableArrayList(
        new XYChart.Data("2015", 70),
        new XYChart.Data("2016", 40),
        new XYChart.Data("2017", 50),
        new XYChart.Data("2018", 30)
));

In this code, I can see a warning. That is,

Unchecked call to 'Data(X, Y)' as a member of raw type 'javafx.scene.chart.XYChart.Data'

Though the code works, I want to remove this warning because I am weak at Java generics programming and want to learn more about it through sample code.

What is the right way of removing that warning?

Thank you.

Bashir
  • 2,057
  • 5
  • 19
  • 44
passion053
  • 367
  • 4
  • 18

2 Answers2

2

Change the lines that look like

new XYChart.Data("2015", 70)

to specify an inferred generic type using the "diamond operator" <>. Like,

new XYChart.Data<>("2015", 70)

and if using an older version of Java (without the diamond operator), provide the <X,Y> type parameters documented in XYChart.Data like

new XYChart.Data<String, Integer>("2015", 70)
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • Shouldn't that be: `XYChart.Series series1 = new XYChart.Series<>();` ? By the way, the OP did mention he was using Java 8, so the diamond operator is supported. – Abra Aug 18 '19 at 06:27
  • @Elliott Frisch, I changed the code as you suggested. The warning that I wanted to remove is removed but new warning is appeared. It tells me that `FXCollections.observableArrayList` has `unchecked generics array creation for varargs parameter`. What is this and how can I remove this warning? Thank you. – passion053 Aug 19 '19 at 15:16
  • @Abra, I changed the code as you suggested and it tells me two warnings. `Unchecked assignment` at `FXCollections.observableArrayList` and `Unchecked call` to `Data()`. – passion053 Aug 19 '19 at 15:19
  • @passion053, [Is it possible to solve the “A generic array of T is created for a varargs parameter” compiler warning?](https://stackoverflow.com/questions/1445233/is-it-possible-to-solve-the-a-generic-array-of-t-is-created-for-a-varargs-param) – Abra Aug 19 '19 at 16:50
0

The following gives no warnings with java 12

XYChart.Series<String, Integer> series1 = new XYChart.Series<>();
ObservableList<XYChart.Data<String, Integer>> list = FXCollections.observableArrayList();
list.add(new XYChart.Data<>("2015", 70));
list.add(new XYChart.Data<>("2016", 40));
list.add(new XYChart.Data<>("2017", 50));
list.add(new XYChart.Data<>("2018", 30));
series1.setData(list);

See my last comment to Elliot's answer. Basically you can't get rid of the warning (other than using the annotation @SuppressWarning) when using method addAll().

Abra
  • 19,142
  • 7
  • 29
  • 41