StackedBarChart上出现空指针异常



我正在尝试使用Java FX(运行1.7.0_45)显示StackedBarChart

我得到以下

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)

我的代码低于

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;

我觉得一切都还好。唯一的问题是,目前,我所有的数据列表都是空的,因此该系列将是空的。也许问题与此有关?

我发现了问题。这实际上是以下代码中的复制粘贴错误

// 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()));
        }

我在didBurnDataList而不是nothingYetDataList中添加内容。因此nothingYetDataList总是空的。无论如何,我不确定这是否是抛出NPE的好理由,因为人们可以想象堆叠条形图的序列可能是空的!

相关内容

  • 没有找到相关文章

最新更新