【问题标题】:getting the buttons in a gridPane and change their text using javaFx获取 gridPane 中的按钮并使用 javaFx 更改其文本
【发布时间】:2016-02-25 14:17:01
【问题描述】:

我试图在我的控制器类中获取 gridPane 的所有成员(它们是按钮),但我遇到了异常,您可以在下面看到它:

Exception in Application start methodjava.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767) 
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$152(LauncherImpl.java:182)
    at com.sun.javafx.application.LauncherImpl$$Lambda$51/1263764.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)
Caused by: javafx.fxml.LoadException: 
file:/C:/Users/Family/Documents/NetBeansProjects/FarsiCallender/dist/run1583526066/FarsiCallender.jar!/farsicallender/FXMLDocument.fxml
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2605)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2583)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2445)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3218)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3179)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3152)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3128)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3108)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3101)
at farsicallender.FarsiCallender.start(FarsiCallender.java:29)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$159(LauncherImpl.java:863)
at com.sun.javafx.application.LauncherImpl$$Lambda$54/4239053.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$172(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl$$Lambda$47/20085625.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$170(PlatformImpl.java:295)
at com.sun.javafx.application.PlatformImpl$$Lambda$49/3796086.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(PlatformImpl.java:294)
at com.sun.javafx.application.PlatformImpl$$Lambda$48/2900468.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$145(WinApplication.java:101)
at com.sun.glass.ui.win.WinApplication$$Lambda$37/17230114.run(Unknown Source)
... 1 more
Caused by: java.lang.NullPointerException
    at farsicallender.FXMLDocumentController.setDays(FXMLDocumentController.java:249)
    at farsicallender.FXMLDocumentController.initialize(FXMLDocumentController.java:181)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2552)
    ... 22 more
Exception running application farsicallender.FarsiCallender
Java Result: 1

此异常来自我的这部分代码,在检查我的代码之前,您必须知道我有一个 gridPane (6*7),这是我的代码:

for (int i = 0; i < 6; i++) {
     for (int j = 0; j < 7; j++) {
         for (Node node : grid.getChildren()) {
             if (grid.getRowIndex(node) == i && grid.getColumnIndex(node) == j) {
                  Button b = (Button)node;
                  b.setText(ar[i][j]+"");
                  System.out.println("z:" + i+" "+j);
                  break;
             }
         }
     }
}

第 249 行是:

if (grid.getRowIndex(node) == i && grid.getColumnIndex(node) == j) {

【问题讨论】:

  • 你能告诉我们哪一行是第 249 行吗?
  • 你在这里...(在我的问题的结尾)
  • 难道你的 IDE 没有给你那条线的警告吗?

标签: java javafx fxml gridpane


【解决方案1】:

顺便说一句,您不应该从非静态上下文中调用静态方法。您的 IDE 应该对此发出警告。但是你应该使用

GridPane.getRowIndex(node)

不是

grid.getRowIndex(node)

等等


方法GridPane.getRowIndex(Node) 和GridPane.getColumnIndex(node) 返回一个包装器Integer 对象,而不是原始int。因此,当您使用== 按身份进行比较时,您会隐式使用拆箱。换句话说,你的代码相当于

if (GridPane.getRowIndex(node).intValue() == i 
  && GridPane.getColumnIndex(node).intValue() == j) { ... }

如果在没有指定行索引和列索引的情况下将任何节点添加到网格窗格中,则 GridPane.getRowIndex(...) 或 GridPane.getColumnIndex(...) 将返回 null,因此您将隐式尝试在 null 引用上调用 intValue(),从而导致在空指针异常中。请注意,如果您调用了grid.setGridLinesVisible(true);,这会导致GridPane 添加一个没有设置行索引或列索引的节点。

因此,快速解决此问题的方法是进行空值检查:

Integer rowIndex = GridPane.getRowIndex(node);
Integer columnIndex = GridPane.getColumnIndex(node);

if (rowIndex != null && rowIndex.intValue() == i
  && columnIndex != null && columnIndex.intValue() == j) {

    // ...
}

我可能更喜欢组织代码,以便将按钮保存在一个数组中,这样您就可以在不进行所有迭代的情况下访问它们,但我想这只是风格问题。

【讨论】:

  • 谢谢。此代码适用于除第一行和第一列之外的每个按钮。
  • 可能第一行第一列的行索引和列索引为空?我认为这会将它们默认为零......我会确保您正确地将行索引和列索引分配给每个按钮。或者,先执行if (rowIndex == null) { rowIndex = 0 ;} if (columnIndex == null) { columnIndex = 0 ;},然后再执行if (rowIndex.intValue() == i &amp;&amp; columnIndex.intValue() == j) { ... }。
猜你喜欢
  • 2017-02-11
  • 2018-08-18
  • 2014-01-13
  • 2014-05-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多