2017-08-29 5 views
0

私はどんな結果なしに変更の多くを作ったjava FXML gridpaneがnullです!どうして?

のJava FXと私は私のgridpaneが nullである理由を理解してくれ(grido.add(tf1,2,2);行で)java.lang.NullPointerExceptionが が発生することはありません開始。

ありがとうございました。

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

'id =" fx:grido "ではなく' fx:id = "grido" 'にする必要があります。 – Itai

+0

これはこれでした!ご回答有難うございます。 – doom

答えて

-1

私はあなたがしたい正確に何を知らないが、まず、あなたのコントローラのニーズが初期化可能インターフェース、および実装を実装メソッドを初期化する。

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

関連する問題