java - 用于在 GridPane 中查找节点位置的代码如何导致 NullPointerException?

标签 java javafx

我正在为 JavaFX 中的简单调度应用程序创建周 View 。该 View 由 8x49 GridPane 组成。 。第一列显示时间,第一行显示星期几,类似于 Google 日历和其他日历软件上的周 View 。当用户更改日期时,我想清除除第一列和第一行之外的网格。我借用了另一个SO问题的答案中的方法。它在我的日 View 面板中完美运行(除了 2x49 网格之外,代码完全相同),但抛出 NullPointerException在我的周 View 面板中。

我添加了两个System.out.println()语句来查看是否有任何实际内容 null 。据我所知,没有什么。我删除了GridPane.getRowIndex(node) == row看看这是否可能是问题所在并且异常不再发生,这让我相信这个语句在某种程度上导致了异常。

这是有问题的方法,借用Shreyas Dave 。我添加了行号以与下面的堆栈跟踪相关:

130: private Node getNodeFromGridPane(GridPane gridPane, int col, int row) {
131:     System.out.println("Gridpane: " + gridPane.getChildren().toString());
132:     for (Node node : gridPane.getChildren()) {
133:         System.out.println("Node: " + node.toString());
134:         if (GridPane.getColumnIndex(node) == col && GridPane.getRowIndex(node) == row) {
135:             return node;
136:         }
137:     }
138:     return null;
139: }

这里是调用 getNodeFromGridPane 的方法:

    private void clearAppointmentsColumn(){
        for(int i = 0; i < 49; i++){
            Node node = getNodeFromGridPane(grid, 1, i+1);
            if(node != null){
                grid.getChildren().remove(node);
            }
        }
    }

这是 System.out.println() 的输出语句和堆栈跟踪的相关部分:

Gridpane: [Label@4b093381[styleClass=label]'Monday', Label@44994e49[styleClass=label]'Tuesday', Label@6f3b59bb[styleClass=label]'Saturday', Label@61dea913[styleClass=label]'Sunday', Label@2f46425[styleClass=label]'Thursday', Label@37370341[styleClass=label]'Wednesday', HBox@3b64a2ab, HBox@3031ce6e, HBox@2e8b5a86, HBox@7d2748fa, HBox@1f40aa1b, HBox@46404c48, HBox@6b829580, HBox@131fffd9, HBox@5a3e3d63, HBox@2f889183, HBox@5d56c9f0, HBox@5f2d13f2, HBox@37bf973, HBox@145f557e, HBox@6db9d36a, HBox@738a8500, HBox@49655482, HBox@75505497, HBox@5b1abd8b, HBox@557f1cad, HBox@223c3dd1, HBox@4d317b8e, HBox@3b093ec, HBox@1183928c]
Node: Label@4b093381[styleClass=label]'Monday'
Node: Label@44994e49[styleClass=label]'Tuesday'
Node: Label@6f3b59bb[styleClass=label]'Saturday'
Node: Label@61dea913[styleClass=label]'Sunday'
Caused by: java.lang.NullPointerException
    at appointmentcalendar.WeekViewController.getNodeFromGridPane(WeekViewController.java:134)
    at appointmentcalendar.WeekViewController.clearAppointmentsColumn(WeekViewController.java:108)
    at appointmentcalendar.WeekViewController.setDate(WeekViewController.java:74)
    at appointmentcalendar.MainWindowController.refreshWeekView(MainWindowController.java:467)
    at appointmentcalendar.MainWindowController.datePicked(MainWindowController.java:455)
    at appointmentcalendar.AppointmentCalendar.switchToMainWindow(AppointmentCalendar.java:90)
    at appointmentcalendar.LoginFormController.loginClicked(LoginFormController.java:71)
    ... 75 more

第 134 行是 if getNodeFromGridPane中的声明.

正如我所说,这个方法在我的 DayViewController 中完美运行。这实际上是相同的代码( WeekViewController.javaWeekView.fxml 是直接复制和粘贴副本,迄今为止唯一的区别是 GridPane 中的列数)。当在 WeekViewController 上运行时,该方法在抛出异常之前恰好处理了 4 个节点。它们每次都是相同的 4 个节点。我尝试从 GridPane 中删除第五个节点结果没有改变。窗口正常显示,应用程序中的其他所有内容都正常运行。我看不出有任何理由会抛出 NullPointerException 。你们能发现我遗漏的东西吗?

更新
事实上,问题在于getRowIndex()方法正在返回 null正如巴斯蒂达所指出的,对于某些节点。将方法修改为以下代码后,问题消失:

    private Node getNodeFromGridPane(GridPane gridPane, int col, int row) {
        System.out.println("Gridpane: " + gridPane.getChildren().toString());
        for (Node node : gridPane.getChildren()) {
            System.out.println("Node: " + node.toString());
            Integer c = GridPane.getColumnIndex(node);
            c = c == null ? 0 : c;
            Integer r = GridPane.getRowIndex(node);
            r = r == null ? 0 : r;
            if (c == col && r  == row) {
                return node;
            }
        }
        return null;
    }

谢谢。

最佳答案

我认为你的问题是将 int 与 Integer 进行比较(它可以处理可能的空值)。

我认为你可以这样做来解决你的问题:

验证 GridPane.getColumnIndex(node)GridPane.getRowIndex(node) 都是除 null 之外的另一个值,如下所示:

private Node getNodeFromGridPane(GridPane gridPane, int col, int row) {
System.out.println("Gridpane: " + gridPane.getChildren().toString());
for (Node node : gridPane.getChildren()) {
    System.out.println("Node: " + node.toString());
    if (GridPane.getColumnIndex(node) != null && GridPane.getRowIndex(node) != null &&
        GridPane.getColumnIndex(node) == col && GridPane.getRowIndex(node) == row) {
        return node;
    }
}
return null;}

希望这对您有帮助:)

关于java - 用于在 GridPane 中查找节点位置的代码如何导致 NullPointerException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55858968/

相关文章:

java - J2ME 中的超链接

JavaFx 自动为动态添加的项目添加监听器

java - 如何在Java中正确创建对方法引用的弱引用

java - 如何提高 ScrollPane JavaFX 的滚动速度

java - jsp页面报错

java - 如何在 Heroku 上公开 HTTPS 端口?

java - 将包含 textview 值的文件 .txt (ArrayList) 中的所有项目设置为 ListView [Android Java]

java - 监听ListView中ScrollBar的变化

java - 在 Java 中调试 - 设置 debugMode 标志

Java 代码可以工作,但不确定是否满足要求(对象和类)