Search code examples
javajavafxnullpointerexceptionscenebuilder

JavaFX controller is null when instantiated


I have been stuck on this for hours. I don't know why my controller from JavaFX is pointing to null. I made a JavaFX GUI in scene builder, added the controller class to my project with its appropriate skeleton code. But when I instantiate the controller in my start method it is pointing to null.

App.java

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

public class App extends Application{
    public static void main(String[] args) {
        launch(args);
    }

    public static SampleController controller;

    @Override
    public void start(Stage stage) throws Exception {
        try {
            FXMLLoader loader = new FXMLLoader(getClass().getResource("testProjectFx.fxml"));
            Parent root = loader.load();

            controller = (SampleController) loader.getController();
            System.out.println("controller is : " + controller);

            Scene scene = new Scene(root, 300, 300);

            stage.setTitle("FXML Welcome");
            stage.setScene(scene);
            stage.show();

        } catch(Exception e){
            e.printStackTrace();
        }
    }
}

SampleController.java

import javafx.fxml.FXML;
import javafx.scene.control.Label;

public class SampleController {

    @FXML
    private Label firstLabel;

}

testProjectFx.fxml

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

<?import javafx.scene.control.Label?>
<?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/16" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <Label fx:id="firstLabel" layoutX="314.0" layoutY="135.0" text="Label" />
   </children>
</AnchorPane>

Error stacktrace

cd /home/hexaquarks/vscode/testProject ; /usr/bin/env /usr/lib/jvm/java-11-openjdk-amd64/bin/java --module-path /home/hexaquarks/Downloads/javafx-sdk-11.0.2/lib --add-modules javafx.controls,javafx.fxml,javafx.swing -Dfile.encoding=UTF-8 @/tmp/cp_s5yrrw0jqsav930qkowo7m19.argfile App 
Aug. 11, 2021 1:30:27 P.M. javafx.fxml.FXMLLoader$ValueElement processValue
WARNING: Loading FXML document with JavaFX API of version 16 by JavaFX runtime of version 11.0.2
controller is : null

with folder tree:

Testproject
├── .vscode
├── bin
├── lib
└── src
    ├── App.java
    ├── SampleController.java
    └── testProjectFx.fxml

I followed the instructions mentionned in here and here, and some other StackOverflow pages. I am reproducing the instructions exactly as they are mentioned but I still get my controller as null.

I am short of ideas, does anyone know what I am doing wrong?


Solution

  • I needed to manually add fx:controller="SampleController" in the root tag of the .fxml document which in my case was the <AnchorPane> tag, now it works.

    <AnchorPane (...) fx:controller="SampleController">
       <children>
          (...)
       </children>
    </AnchorPane>
    

    If one is using "Scene Builder" then go in the bottom left "controller" tab and input in the text field "controller class" the chosen instance name that is given in the start() method.