Search code examples
javajavafxnullpointerexceptionscenebuilderjcomponent

JavaFX - Components are null when initializing Controller


Main class :

Parent root = FXMLLoader.load(getClass().getResource("../layouts/main_window.fxml"));
primaryStage.setTitle("IMGManager");
primaryStage.setScene(new Scene(root));
primaryStage.show();

FXML :

<VBox id="navigation" stylesheets="@../../css/center_panels.css" 
      xmlns="http://javafx.com/javafx/8" 
      xmlns:fx="http://javafx.com/fxml/1" 
      fx:controller="controllers.NavigationController">

  <!-- ... -->

  <!-- List -->
  <AnchorPane prefHeight="1500.0" prefWidth="492.0">
      <children>
          <ListView id="directories-list" 
                    fx:id="navigationList" 
                    prefHeight="500.0" ... />
      </children>
  </AnchorPane>

</VBox>

Controller : NavigationController

public class NavigationController implements Initializable {

    @FXML private ListView<String> navigationList;

    // ...

    @Override
    public void initialize(URL location, ResourceBundle resources) {

        // Get Home Directory
        FileSystemView fsw = ...

        // Updates the information displayed in the Navigation panel.
        if (navigationList != null)
        {
            updateNavigationDisplay();
        }
    }

    // ...
}

Hi, so the problems I have is :

1. )

Whenever I try to link a JavaFX component and a Controller variable, neither the Scene Builder nor Intellij makes the "connection" between the fx:id(s) and the variables in my Controllers.

I always get the nullPointerError in the initialization of my Controller, but I found a (troublesome) workaround : if (navigationList != null).

I noticed that my initialization method fires twice. Once with null components and the second time (magic), components are detected. So by adding if (navigationList != null), I don't get any errors on the first initialization and on the second I do cool stuffs.

2. )

I am unable to access a component located in another FXML file then the one linked with the Controller : nullPointer.

ex:

Controller1 <--linked with this, works well (on second init...) --> FXML1.fxml

Controller1 -- trying to access components in --> FXML2.fxml ( fails miserably)

EDIT:

By "trying to access components in another fxml file", I mean with a mouse click or something like that -> And surely Not during the initialization, as some fxml files are not loaded yet.

I searched a lot for answers and lost a couple of hours on this. Most of the time, smilar cases were solved by correcting some small misspells, or missing @FXML tags or by adding a proper implementation of the Intializable interface, etc.

In my case, I really don't get it. Hopefully it's just a misspell or some small bugs.

Thanks for your answers


Solution

  • Problem 1 : have you added given FXML directly in main_window.fxml or you have included this FXML in main_window.fxml?

    Problem 2 : To access components located in another fxml through your controller of first fxml please refer this link : https://www.youtube.com/watch?v=NgubWgheboI

    I was facing same problem.:)