Search code examples
javafx-2stackedbarseries

Null pointer exception on StackedBarChart


I'm trying to display a StackedBarChart with Java FX (running 1.7.0_45)

I get the following

java.lang.NullPointerException
    at javafx.scene.chart.StackedBarChart.getDataItem(StackedBarChart.java:500)
    at javafx.scene.chart.StackedBarChart.updateAxisRange(StackedBarChart.java:390)
    at javafx.scene.chart.XYChart.layoutChartChildren(XYChart.java:620)
    at javafx.scene.chart.Chart$1.layoutChildren(Chart.java:84)
    at javafx.scene.Parent.layout(Parent.java:1018)

My code is below

public StackedBarChart<String, Number> createTreeNbChart() {
        String[] typeString = new String[TypeE.values().length];
        for (int i = 0; i < typeString.length; i++) {
            typeString[i] = TypeE.values()[i].toString();
        }
        final CategoryAxis xAxis = CategoryAxisBuilder
                .create()
                .label("Type")
                .categories(
                        FXCollections.<String> observableArrayList(typeString))
                .build();
        final NumberAxis yAxis = NumberAxisBuilder.create().label("Nb").build();

        List<Tree> didBurn = new ArrayList<Tree>();
        List<Tree> didGive = new ArrayList<Tree>();
        List<Tree> nothingYet = new ArrayList<Tree>();
        this.organizeTreesInto(didBurn, didGive, nothingYet);

        // Trees which did give
        ArrayList<StackedBarChart.Data<String, Number>> giveDataList = new ArrayList<>();
        for (TypeE type : TypeE.values()) {
            giveDataList.add(new StackedBarChart.Data<String, Number>(
                    type.toString(), this.treesFromListWithType(didGive, type)
                            .size()));
        }
        StackedBarChart.Series<String, Number> didGiveSeries = new StackedBarChart.Series<String, Number>(
                "ont donné",
                FXCollections.observableArrayList(giveDataList));

        // Trees which did burn
        ArrayList<StackedBarChart.Data<String, Number>> didBurnDataList = new ArrayList<>();
        for (TypeE type : TypeE.values()) {
            didBurnDataList.add(new StackedBarChart.Data<String, Number>(type
                    .toString(), this.treesFromListWithType(didBurn, type)
                    .size()));
        }
        StackedBarChart.Series<String, Number> didBurnSeries = new StackedBarChart.Series<String, Number>(
                "ont brûlé", FXCollections.observableArrayList(didBurnDataList));

        // Trees with nothing yet
        ArrayList<StackedBarChart.Data<String, Number>> nothingYetDataList = new ArrayList<>();
        for (TypeE type : TypeE.values()) {
            didBurnDataList.add(new StackedBarChart.Data<String, Number>(type
                    .toString(), this.treesFromListWithType(nothingYet, type)
                    .size()));
        }
        StackedBarChart.Series<String, Number> nothingYetSeries = new StackedBarChart.Series<String, Number>(
                "les autres",
                FXCollections.observableArrayList(nothingYetDataList));

        ObservableList<StackedBarChart.Series<String, Number>> barChartData = FXCollections
                .observableArrayList(didGiveSeries, didBurnSeries,
                        nothingYetSeries);
        final StackedBarChart<String, Number> chart = new StackedBarChart<String, Number>(
                xAxis, yAxis, barChartData);
        return chart;

Everything looks OK to me. The only thing is that for the moment, all my data lists are empty, therefore the series will be empty. Maybe the problem is linked to that ?


Solution

  • I found the problem. It was actually a copy-paste mistake in the following code

    // Trees with nothing yet
            ArrayList<StackedBarChart.Data<String, Number>> nothingYetDataList = new ArrayList<>();
            for (TypeE type : TypeE.values()) {
                didBurnDataList.add(new StackedBarChart.Data<String, Number>(type
                        .toString(), this.treesFromListWithType(nothingYet, type)
                        .size()));
            }
    

    I was adding stuff in the didBurnDataList instead of the nothingYetDataList. Therefore the nothingYetDataList was always empty. I'm not sure this is a good reason to throw a NPE anyway, since one can think of a scenario where Series of a stacked bar chart can be empty !