Search code examples
javajavafxnullpointerexceptionfxml

Cannot seem to instantiate a non-null FXMLLoader


I have read a few of the related posts on this topic but have been unable to use them to solve my issue. I believe my failure is one of comprehension and not that I am facing a unique issue. But, I am at a total impasse.

I'm building a CRUD application using JavaFX. One of my application's buttons, "Import Data," is throwing NullPointerExceptions:

// *a button that opens a new window with a textField where the user can paste text data*
@FXML
private void importDataButton(ActionEvent event) {
    // *load the fxml file*
    URL viewLocation = getClass().getResource("/importView.fxml");
    // *get the file's controller*
    FXMLLoader loader = new FXMLLoader();
    ImportController importController = loader.getController();
    importController.setMainController(this);
    loader.setLocation(viewLocation);

    try {
        loader.load();
    } catch (IOException exception) {
        System.out.println("IO Exception thrown.");
        return;
    }
....
}

I'm not very good with IntelliJ's debugger yet, but I've used it to determine that the FXMLLoader object is null. So when

importController.setMainController(this);

executes, a NullPointerException is thrown because the object this refers to is null. I...think. So then

ImportController importController = loader.getController();

cannot retrieve the controller from the FXMLLoader object (loader).

For reference, setMainController() is in another class called ImportController, and that method's code is as follows:

public void setMainController(MainController mainController) {
    this.mainController = mainController;
}

Things I've tried:

I read this post and this post, both of them recommend that I must run loader.load() [given FXMLLoader loader = new FXMLLoader()] in order to retrieve data from the object. However, I've tried this, and I just get errors upon errors: InvocationTargetExceptions and IllegalStateExceptions. I have also tried instantiating an FXMLLoader object that is non-null, using

FXMLLoader load = new FXMLLoader(getClass.getResource("sample.fxml"));

But it seems to have no effect on the content of the object (and yes, I am replacing "sample.fxml" with my filename.)

I hate to make a similar post, but I have no idea what to do.


Solution

  • The problem is that you are calling loader.getController() before loader.load(). That's why your importController is null when calling importController.setMainController(this).

    Call loader.load() first:

    URL viewLocation = getClass().getResource("/importView.fxml");
    FXMLLoader loader = new FXMLLoader(viewLocation);
    
    try {
        loader.load();
    
        ImportController importController = loader.getController();
        importController.setMainController(this);
    } catch (IOException exception) {
        exception.printStackTrace();
    }
    

    But be aware that the initialize() method in your ImportController is called before setMainController().