2017-08-29 2 views
0

I Java FX starten und ich verstehe nicht, warum meine gridpane null und verursacht mir eine java.lang.NullPointerException (an der grido.add(tf1,2,2); Linie)Java FXML-Gridpane ist null! Warum?

ich ohne Ergebnis viel Änderung vorgenommen haben.

Danke für Ihre Hilfe.

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

} 

Ausgabe lautet:

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

Es sollte 'fx: id =" grido "' statt id = "fx: grido" 'sein. – Itai

+0

Es war das! Danke für deine Antwort. – doom

Antwort

-1

Ich weiß nicht genau, was Sie wollen, aber erstens Ihre Bedürfnisse Controller initialisierbare Schnittstelle und Geräte implementieren Initialisierungsmethode

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

Es ist keineswegs notwendig, 'Initialisierbar' zu implementieren. – Itai

+0

In wenigen Worten: Die Methode wird zuerst aufgerufen, dann werden alle mit ** @ FXML ** annotierten Felder ausgefüllt. Diese Methode ** addRow ** hat also NICHT Zugriff auf ** FXML-Felder **, die sich auf Komponenten beziehen, die in der .fxml-Datei definiert sind, während initialize() darauf zugreifen kann. –

+1

Die Methode 'initialize' wird in der Tat aufgerufen, nachdem alle mit '@ FXML' annotierten Felder injiziert wurden, aber ihre Existenz ist nicht notwendig, damit sie injiziert werden können. Nachdem sie injiziert wurden, sind sie für jede Methode der Controller-Klasse zugänglich. Bei der ursprünglichen Frage tritt der Aufruf von 'addRow' auf, nachdem der Aufruf von' FXMLLoader # load' zurückkehrt, so dass die Ausführung nach '@ FXML'-Injektionen garantiert ist. – Itai