【发布时间】:2020-03-10 15:21:05
【问题描述】:
我这样设置网格窗格:
for (int row = 0; row < mainBoard.getRows(); row++){
for (int column = 0; column < mainBoard.getColumns(); column++) {
mainBoard.setStatusArray(row, column, 0);
Pane pane = new Pane();
GridPane.setRowIndex(pane, row);
GridPane.setColumnIndex(pane, column);
gridpane.getChildren().add(new Pane());
System.out.println(GridPane.getRowIndex(pane));
}
}
并尝试像这样访问它:
ObservableList<Node> childrens = gridpane.getChildren();
for (Node node : childrens) {
if (node instanceof Pane
&& GridPane.getRowIndex(node) == 1 <------------- line of error
&& getColumnIndex(node) == 1) {
// Do stuff
}
}
似乎GridPane.getRowIndex(node) 总是返回null,从而导致NullPointerException,我终生无法弄清楚为什么。
【问题讨论】:
-
我得到
null返回的唯一时间是网格窗格是否包含其他未设置列和行索引的子节点(例如,如果您有可见的网格线)。如果您只提供空检查,您的代码应该可以正常工作。