2017-08-29 2 views
0

Je commence Java FX et je ne comprends pas pourquoi mon gridpane est nulle et fais-moi un java.lang.NullPointerException (à la ligne grido.add(tf1,2,2);)java FXML gridpane est null! Pourquoi?

J'ai fait beaucoup de changements sans aucun résultat.

Merci pour votre aide.

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 !"); 
    } 

} 

sortie est:

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

Il devrait être 'fx: id = "Grido"' plutôt que 'id = "fx: Grido"'. – Itai

+0

C'était ça! Merci pour votre réponse. – doom

Répondre

-1

Je ne sais pas exactement ce que vous voulez faire, mais tout d'abord vos besoins de contrôleur mettre en œuvre Initializable interface et outils initialiser la méthode.

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

Il n'est en aucun cas nécessaire d'implémenter 'Initializable'. – Itai

+0

En quelques mots: La méthode est appelée en premier, puis tous les champs annotés ** @ FXML ** sont remplis. Donc, cette méthode ** addRow ** n'a pas accès aux champs ** @ FXML ** se référant aux composants définis dans le fichier .fxml, alors que initialize() y a accès. –

+1

La méthode 'initialize' est en effet appelée après que tous les champs annotés' @ FXML' ont été injectés, mais son existence n'est pas nécessaire pour les injecter. Après avoir été injectés, ils sont accessibles par n'importe quelle méthode de la classe de contrôleur. Dans le cas de la question d'origine, l'appel de 'addRow' se produit après le retour de l'appel de' FXMLLoader # load', donc il est garanti de s'exécuter après les injections '@ FXML'. – Itai