2017-08-29 42 views
0

我啓動Java FX,我不明白爲什麼我的gridpane爲空 並導致我顯示java.lang.NullPointerException (在grido.add(tf1,2,2);線)java FXML gridpane爲null!爲什麼?

我已經做了很大的改變,沒有任何結果。

感謝您的幫助。

keepSpaceFXML.fxml

<?xml version="1.0" encoding="UTF-8"?> 

<?import java.lang.*?> 
<?import javafx.scene.control.*?> 
<?import javafx.scene.layout.*?> 

<GridPane id="fx:grido" xmlns:fx="http://javafx.com/fxml/1"> 
    <Button GridPane.columnIndex="0" GridPane.rowIndex="0" text="coucou2" onAction="#handleButtonAction" fx:id="button" /> 
</GridPane> 

KeepSpaceFXML.java

package keepspacefxml; 

import javafx.application.Application; 
import javafx.fxml.FXMLLoader; 
import javafx.scene.Parent; 
import javafx.scene.Scene; 
import javafx.stage.Stage; 
import javafx.scene.layout.GridPane; 

public class KeepSpaceFXML extends Application { 

    @Override 
    public void start(Stage stage) throws Exception { 
     FXMLLoader loader=new FXMLLoader(); 
     loader.setLocation(getClass().getResource("keepSpaceFXML.fxml")); 
     KeepSpaceGridController gridController = new KeepSpaceGridController(); 
     loader.setController(gridController); 
     Parent root = loader.load(); 
     System.err.println("gridController"); 
     System.err.println(gridController); 
     Scene scene = new Scene(root); 


     stage.setScene(scene); 
     stage.show(); 
     gridController.addRow("hello gridpane !"); 
    } 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
     launch(args); 
    } 

} 

KeepSpaceGridController.java

package keepspacefxml; 

import java.net.URL; 
import javafx.event.ActionEvent; 
import javafx.fxml.FXML; 
import javafx.scene.control.Label; 
import javafx.scene.control.TextField; 
import javafx.scene.layout.GridPane; 

public class KeepSpaceGridController { 

    @FXML public GridPane grido; 

    @FXML private Label label; 

    public void addRow(String text) { 
    System.out.println("add row"); 
    TextField tf1 = new TextField("tf 1"); 
     System.err.println("grido"); 
     System.err.println(grido); 
    grido.add(tf1,2,2); 

    } 

    public void setLabel(String text) { 
     label.setText(text); 
    } 


    @FXML 
    private void handleButtonAction(ActionEvent event) { 
     System.out.println("clicked !"); 
    } 

} 

輸出爲:

ant -f /home/doom/Documents/javafxtests/keepSpaceFXML jfxsa-run 
init: 
Deleting: /home/doom/Documents/javafxtests/keepSpaceFXML/build/built-jar.properties 
deps-jar: 
Updating property file: /home/doom/Documents/javafxtests/keepSpaceFXML/build/built-jar.properties 
Compiling 1 source file to /home/doom/Documents/javafxtests/keepSpaceFXML/build/classes 
compile: 
Detected JavaFX Ant API version 1.3 
Launching <fx:jar> task from /home/doom/jdk1.8.0_141/jre/../lib/ant-javafx.jar 
Warning: From JDK7u25 the Codebase manifest attribute should be used to restrict JAR repurposing. 
     Please set manifest.custom.codebase property to override the current default non-secure value '*'. 
Launching <fx:deploy> task from /home/doom/jdk1.8.0_141/jre/../lib/ant-javafx.jar 
No base JDK. Package will use system JRE. 
No base JDK. Package will use system JRE. 
jfx-deployment-script: 
jfx-deployment: 
jar: 
Copying 12 files to /home/doom/Documents/javafxtests/keepSpaceFXML/dist/run2075630249 
jfx-project-run: 
Executing /home/doom/Documents/javafxtests/keepSpaceFXML/dist/run2075630249/keepSpaceFXML.jar using platform /home/doom/jdk1.8.0_141/jre/bin/java 
gridController 
[email protected] 
add row 
grido 
null 
Exception in Application start method 
java.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:498) 
    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:498) 
    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$155(LauncherImpl.java:182) 
    at java.lang.Thread.run(Thread.java:748) 
Caused by: java.lang.NullPointerException 
    at keepspacefxml.KeepSpaceGridController.addRow(KeepSpaceGridController.java:51) 
    at keepspacefxml.KeepSpaceFXML.start(KeepSpaceFXML.java:38) 
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863) 
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326) 
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294) 
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95) 
    at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method) 
    at com.sun.glass.ui.gtk.GtkApplication.lambda$null$49(GtkApplication.java:139) 
    ... 1 more 
Exception running application keepspacefxml.KeepSpaceFXML 
Java Result: 1 
Deleting directory /home/doom/Documents/javafxtests/keepSpaceFXML/dist/run2075630249 
jfxsa-run: 
BUILD SUCCESSFUL (total time: 1 second) 
+2

它應該是'fx:id =「grido」'而不是'id =「fx:grido」'。 – Itai

+0

這是這個!感謝您的回答。 – doom

回答

-1

我不知道你想做的事,但首先你的控制器需要實現Initializable接口,並實現什麼初始化方法。

import java.net.URL; 
import java.util.ResourceBundle; 
import javafx.event.ActionEvent; 
import javafx.fxml.FXML; 
import javafx.fxml.Initializable; 
import javafx.scene.control.Label; 
import javafx.scene.control.TextField; 
import javafx.scene.layout.GridPane; 

public class KeepSpaceGridController implements Initializable { 

    @FXML 
    public GridPane grido; 

    @FXML 
    private Label label; 

    @Override 
    public void initialize(URL location, ResourceBundle resources) { 
     addRow("add row"); 
    } 

    public void addRow(String text) { 
     System.out.println("add row"); 
     TextField tf1 = new TextField("tf 1"); 
     grido.add(tf1, 2, 2); 

    } 

    public void setLabel(String text) { 
     label.setText(text); 
    } 

    @FXML 
    private void handleButtonAction(ActionEvent event) { 
     System.out.println("clicked !"); 
    } 

} 
+1

實現'Initializable'沒有必要。 – Itai

+0

簡而言之:首先調用該方法,然後填充任何** @ FXML **帶註釋的字段。因此,此方法** addRow **無法訪問指向.fxml文件中定義的組件的** @ FXML字段**,而initialize()確實可以訪問它們。 –

+1

''initialize'的方法確實在所有注入@ FXML註解的字段被調用之後調用,但是它的存在並不是必須注入的。注入後,它們可以通過控制器類的任何方法訪問。 在原始問題的情況下,對'addRow'的調用發生在對FXMLLoader#load的調用返回後,因此保證在'@ FXML'注入之後執行。 – Itai