-1

I get a NullPointerException in JavaFX when initializing my FXML in Main class.

I call in every Controller class the Main function initProgramm() and count the calls.

@Override
public void initialize(URL url, ResourceBundle rb) {

    Main.initProgramm();
} 

Main class

public class Main extends Application {

    public static Stage stage;
    public static String language;
    public static ControllerMain controllerMain;
    public static ControllerTabManual controllerTabManual;
    public static ControllerTabCode controllerTabCode;

    private static int countInits=0;

    @Override
    public void start(Stage stage) throws Exception {

        try( Scanner file = new Scanner(new FileInputStream(new File("res/settings/language.txt")), StandardCharsets.UTF_8.name()) ) {
            language = file.nextLine().trim();
        }
        catch(Exception e){}

        FXMLLoader loader = new FXMLLoader(getClass().getResource("fxml/Main.fxml"));
        Parent root = (Parent) loader.load();

        controllerMain = (ControllerMain) loader.getController();
        controllerTabManual = (ControllerTabManual) loader.getController();
        controllerTabCode = (ControllerTabCode) loader.getController();

        Scene scene = new Scene(root);
        scene.getStylesheets().add(getClass().getResource("css/default.css").toExternalForm());

        stage.getIcons().addAll(
            new Image(getClass().getResourceAsStream("img/app_icon64x64.png")),
            new Image(getClass().getResourceAsStream("img/app_icon32x32.png")),
            new Image(getClass().getResourceAsStream("img/app_icon16x16.png"))
        );
        stage.setTitle("stepControl");
        stage.setScene(scene);
        stage.setOnCloseRequest(e -> {
                e.consume();
                closeProgramm();
            }
        );
        stage.show();

        Main.stage = stage;
    }

    public static void initProgramm() {

        countInits++;

        if( countInits==3 )
        {
            if( Main.language.equals("de") )
            controllerMain.menuLanguageOne.setDisable(true);  // LINE 71 
            else if( Main.language.equals("en") )
            controllerMain.menuLanguageTwo.setDisable(true);


            controllerMain.grblSender = new GrblSender();    
            controllerMain.grblSender.setPorts(controllerTabManual.comboBoxSelectPort, true);
        }
    }

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

Last error:

Caused by: java.lang.NullPointerException
    at de.emericon.stepcontrol.Main.initProgramm(Main.java:71)
    at de.emericon.stepcontrol.ControllerMain.initialize(ControllerMain.java:55)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
    ... 12 more
Exception running application de.emericon.stepcontrol.Main

I think the root cause for the exception is the referencing of the controller classes?!

    public static ControllerMain controllerMain;
    public static ControllerTabManual controllerTabManual;
    public static ControllerTabCode controllerTabCode;
Luke
  • 59
  • 7
  • 1
    First of all: One FXML file shall have one controller. The FXML Loader will create a controller that your are referencing in the FXML file. One controller, with one type. Therefore it is impossible to parse to 3 different types. And also, it makes no sense. – DVarga Apr 12 '16 at 20:45
  • 1
    To add to @DVarga's comment: it is really unclear what you are even trying to do here. Why are there three different controller classes? What are you actually expecting this to do? Why don't you just put the code from your `initProgramm` method in the `initialize()` method of the controller? – James_D Apr 12 '16 at 22:30

1 Answers1

0
Parent root = (Parent) loader.load();

This will call the initialize method of your controller (ControllerMain);

In this method you call initProgramm. And in this method you call controllerMain.menuLanguageOne.setDisable(true); // LINE 71.

So here you want to use controllerMain. BUT!

controllerMain = (ControllerMain) loader.getController();

controllerMain variable gets value here. But this line in not reached at this point, because it is after the Parent root = (Parent) loader.load(); line.

Therefore, you have a nice NullPointerException.

But, please read my comment on the OP.

Mike Bonnell
  • 16,181
  • 3
  • 61
  • 77
DVarga
  • 21,311
  • 6
  • 55
  • 60