title: JavaFX load FXML inside parent controller
body: <p>I have a borderpane where I'm loading an fxml (alias FirstPanel) with relative controller inside of it and positioned in the center.  The fxml contains a TableView and a button which should load another fxml (alias SecondPanel) and relative controller instead the first panel. Basically, the SecondPanel needs to show some details about the data selected in the table.</p>
  
  <p>Is it possible to do it? How can I get the parent of my FirstPanel and use it for the SecondPanel instead of the first?   </p>
  
  <p>UPDATE</p>
  
  <p>I've tried many solutions but without reach my goal. The application load the UsersMainPageController which contains only an AnchorPane like parent control, so this is the relative code:</p>
  
  <p>[UsersMainPageController]</p>
  
  <pre><code>public class UsersMainPageController implements Initializable {
  
      private PostOffice application;
  
      @FXML
      private AnchorPane ParentControl;
  
      public void setApp(PostOffice application){
          this.application = application;
      } 
  
      public void loadPage(String pageName) {
          try {                    
              URL url = getClass().getResource(pageName);
              FXMLLoader fxmlLoader = new FXMLLoader();
              fxmlLoader.setLocation(url);
              fxmlLoader.setBuilderFactory(new JavaFXBuilderFactory());
              AnchorPane page = (AnchorPane) fxmlLoader.load(url.openStream()); 
  
              ParentControl.getChildren().clear();///name of pane where you want to put the fxml.
              ParentControl.getChildren().add(page);
  
          } 
          catch (IOException e) {
              e.printStackTrace();
          }
      }
  
      public void loadManageUsers () {
          loadPage("UsersManage.fxml");
      }
  
      public void loadListUsers () {
          loadPage("UsersList.fxml");
      }
  
      /**
       * Initializes the controller class.
       */
  
      @Override
      public void initialize(URL url, ResourceBundle rb) {
          loadListUsers();
      }
  
  }
  </code></pre>
  
  <p>loadListUsers calls UsersList.fxml with the relative controller UsersListController that contains a TableView with some records and some buttons. When I click a specific button, it should call loadManageUsers with relative controller UsersManageController which contains some fields for editing data and inserting new users. When users are edited or inserted, it should be able to return to the previous page with the TableView and clear the current page (in this case UsersManageController).</p>
  
  <p>[UsersListController]</p>
  
  <pre><code>public class UsersListController implements Initializable {
      private UsersMainPageController mainController;
  
      @FXML
      private void handleButtonEditAction(ActionEvent event) throws IOException {
          mainController.loadManageUsers();
      }
  }
  </code></pre>
  
  <p>[UsersManageController]</p>
  
  <pre><code>public class UsersManageController implements Initializable {
      private UsersMainPageController mainController;
  
      @FXML
      private void handleButtonBackAction(ActionEvent event) throws IOException {
      mainController.loadListUsers();
      }
  }
  </code></pre>
  
  <p>When I click from the <code>UsersListController</code> the <code>ButtonEdit</code> to load the <code>UsersManageController</code>, I get this error:</p>
  
  <pre><code>Caused by: java.lang.NullPointerException
      at postoffice.multiuser.UsersListController.handleButtonAggiornaAction(UsersListController.java:210)
      ... 50 more
  </code></pre>