0

I'm currently trying to learn javafx, but I keep getting this error: java.lang.ClassCastException. I want to add String values to the xAxis and Integer values to the yAxis. This is my code:

    @Override
public void start(Stage window) {
    try {
        List<Map<String, LinkedHashMap<String, Integer>>> data = fileReader("Master-Law-Male&&Female.txt");
        CategoryAxis xAxis = new CategoryAxis();
        NumberAxis yAxis = new NumberAxis(0, 3000, 30);
        
        LineChart lineChart = new LineChart(xAxis, yAxis);
        lineChart.setTitle("Number of people with master degree in law, in switzerland.");
        
        data.stream().forEach(map -> {
            map.entrySet().stream().forEach(lhm -> {
                XYChart.Series line = new XYChart.Series();
                line.setName(lhm.getKey());
                for(Entry<String, Integer> i : lhm.getValue().entrySet()) {
                    line.getData().add(new XYChart.Data(lhm.getKey(), lhm.getValue()));
                }
                lineChart.getData().add(line);
            });
            
        });
        Scene scene = new Scene(lineChart);
        window.setScene(scene);
        window.show();
    } catch(Exception e) {
        e.printStackTrace();
    }
}

Strangely though if I iterate through the values, I'm getting the results I want:

List<Map<String, LinkedHashMap<String, Integer>>> data = fileReader("Master-Law-Male&&Female.txt");
    Map<String, LinkedHashMap<String, Integer>> test = data.get(0);


    data.stream().forEach(map -> {
        map.entrySet().stream().forEach(lhm -> {
            for(Entry<String, Integer> i : lhm.getValue().entrySet()) {
                System.out.println(i.getKey() + " - " + i.getValue());
            }
        });
    });

And these are some examples printed in the command line:

"2001/02" - 0
"2002/03" - 0
"2003/04" - 6
"2004/05" - 222
"2005/06" - 563
"2006/07" - 1018
"2007/08" - 1215

Sec1
  • 1
  • 1
    On what line does the exception occur? What does the exception message say? – Andy Turner Jun 10 '22 at 18:44
  • 1
    It occurs on the line: "window.show()" and the error message says: "class java.util.LinkedHashMap cannot be cast to class java.lang.Number (java.util.LinkedHashMap and java.lang.Number are in module java.base of loader 'bootstrap')" – Sec1 Jun 10 '22 at 18:49

0 Answers0