0

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

我得到以下

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;

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

4

1 回答 1

0

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

// 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 的好理由,因为人们可以想到堆叠条形图系列可能为空的场景!

于 2013-11-26T19:19:37.480 回答