2016-02-25 72 views
0

我試着去得到一個gridPane(他們是按鈕)在我的控制器類的所有成員改變他們的文字,但我發現了異常,您可以在下面看到它:獲得按鈕在gridPane和使用JavaFX

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) { 
+0

你能告訴我們哪一行是249行嗎? –

+0

在這裏你是...(在我的問題結束時) –

+0

不是你的IDE給你這條線的警告嗎? –

回答

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,所以你會隱式地試圖調用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) { 

    // ... 
} 

我可能更願意組織代碼,以便按鈕被保存在一個數組,這樣你就可以在沒有所有迭代訪問它們,但我想這只是一個風格問題。

+0

謝謝。此代碼適用於除第一行和第一列以外的每個按鈕。 –

+0

也許行索引和列索引對於第一行和第一列是空的?我認爲這會默認他們爲零...我只是確保你正確地分配行索引和列索引到每個按鈕。或者,if(rowIndex == null){rowIndex = 0;} if(columnIndex == null){columnIndex = 0;}'然後只是if(rowIndex.intValue()== i && columnIndex.intValue )== j){...}'。 –