我正在尝试使用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;
一切看起来都不错。 唯一的问题是,目前我的所有数据列表都是空的,因此系列将为空。也许问题与此有关?
答案 0 :(得分: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的理由,因为人们可以想到一个堆叠条形图系列可能是空的情况!