0

I'm trying to build a small application with JavaFX. My problem is that the field annotated with @FXML are not getting injected, leaving me with a lot of null pointers.

public class TestUIController extends Application {


@FXML private TextField testTextField;
@FXML private Button testButton;


public static void main(String[] args) {
    Application.launch(args);
}

@Override
public void start(Stage primaryStage) throws Exception {
    try {
        AnchorPane page = (AnchorPane) FXMLLoader.load(TestUIController.class.getResource("TestFXML.fxml"));
        Scene scene = new Scene(page);
        primaryStage.setScene(scene);
        primaryStage.setTitle("FXML is Simple");
        primaryStage.show();
    } catch (Exception ex) {
        //Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        System.out.println("fuck this and fuck that");

    }

    System.out.println("TextField: " + testTextField);
    System.out.println("Button: " + testButton);

}

If I read the Tutorials and Documentation right, my fields testTextField and testButton should get Initialized by the FXMLLoader in the load command, because i annotated them and specified the fx:id in the .fxml file.

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>


<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="test.TestUIController">
   <children>
      <Button fx:id="testButton" layoutX="412.0" layoutY="169.0" mnemonicParsing="false" text="Button" />
      <TextField fx:id="testTextField" layoutX="206.0" layoutY="169.0" />
   </children>
</AnchorPane>

Has anyone else Experienced this?

Stack Trace:

java.lang.Thread.getStackTrace(Thread.java:1552)
test.TestUIController.start(TestUIController.java:45)
com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
java.security.AccessController.doPrivileged(Native Method)
com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
java.lang.Thread.run(Thread.java:745)

0 Answers0