title: JavaFX 8 - Tabpanes and tabs with separate FXML and controllers for each tab
body: <p>I hope to get some answers regarding having fx:include statements for each tab in a tabpane. I have managed with ease to get content to show up BUT referencing methods of the associated controller class simply gives me a nullpointerreference exception no matter how I structure it. The controllers of the included FXML layouts do not have neither constructor not initialize() methods, are they needed? I tried some different things but always got the same exception.</p>
  
  <p>What I simply did was add a change listener to the tabpane and when a tab was pressed I wanted to populate some textfields with some values gotten from a globally accessible arraylist. Note: the arraylist is not the issue, performing this operation using the main controller works fine. </p>
  
  <p>I'm going to add a code example shortly but cannot right now. Please let me know if you need more info, otherwise I'll post the code later today. </p>
  
  <p>*Edit, here is my code example, taken from another thread here on StackOverflow.
  <a href="https://stackoverflow.com/questions/19889882/javafx-tabpane-one-controller-for-each-tab">JavaFX TabPane - One controller for each tab</a></p>
  
  <p>TestApp.java:</p>
  
  <pre><code>public class TestApp extends Application {
      @Override
      public void start(Stage primaryStage) throws Exception {
          Scene scene = new Scene(new StackPane());
  
          FXMLLoader loader = new FXMLLoader(getClass().getResource("/view/MainTestWindow.fxml"));
          scene.setRoot(loader.load());
          MainTestController controller = loader.getController();
          controller.init();
  
          primaryStage.setScene(scene);
          primaryStage.show();
      }
  
      public static void main(String[] args) {
          launch(args);
      }
  }
  </code></pre>
  
  <p>Main controller, where I want to reference the sub controllers.</p>
  
  <pre><code>public class MainTestController {
  
      @FXML private TabPane tabPane;
      // Inject tab content.
      @FXML private Tab fooTabPage;
      // Inject controller
      @FXML private FooTabController fooTabPageController;
  
      // Inject tab content.
      @FXML private Tab barTabPage;
      // Inject controller
      @FXML private BarTabController barTabPageController;
  
      public void init() {
          tabPane.getSelectionModel().selectedItemProperty().addListener((ObservableValue&lt;? extends Tab&gt; observable,
                                                                          Tab oldValue, Tab newValue) -&gt; {
              if (newValue == barTabPage) {
                  System.out.println("Bar Tab page");
                  barTabPageController.handleButton();
              } else if (newValue == fooTabPage) {
                  System.out.println("Foo Tab page");
                  fooTabPageController.handleButton();
              }
          });
      }
  }
  </code></pre>
  
  <p>Main view's .fxml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  &lt;?import javafx.scene.control.TabPane?&gt;
  &lt;?import javafx.scene.control.Tab?&gt;
  
  &lt;TabPane fx:id="tabPane" fx:controller="controller.MainTestController" xmlns="http://javafx.com/javafx/8.0.40"
           xmlns:fx="http://www.w3.org/2001/XInclude"&gt;
      &lt;tabs&gt;
          &lt;Tab fx:id="fooTabPage" text="FooTab"&gt;
              &lt;fx:include source="fooTabPage.fxml"/&gt;
          &lt;/Tab&gt;
          &lt;Tab fx:id="barTabPage" text="BarTab"&gt;
              &lt;fx:include source="barTabPage.fxml"/&gt;
          &lt;/Tab&gt;
      &lt;/tabs&gt;
  &lt;/TabPane&gt;
  </code></pre>
  
  <p>FooTab:</p>
  
  <pre><code>public class FooTabController {
      @FXML private Label lblText;
  
      public void handleButton() {
          lblText.setText("Byebye!");
      }
  }
  </code></pre>
  
  <p>FooTab's .fxml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  &lt;?import javafx.scene.layout.VBox?&gt;
  &lt;?import javafx.scene.control.Label?&gt;
  
  &lt;?import javafx.scene.control.Button?&gt;
  &lt;VBox xmlns:fx="http://www.w3.org/2001/XInclude" fx:controller="controller.FooTabController"&gt;
      &lt;Label fx:id="lblText" text="Helllo"/&gt;
      &lt;Button fx:id="btnFooTab" onAction="#handleButton" text="Change text"/&gt;
  &lt;/VBox&gt;
  </code></pre>
  
  <p>BarTab:</p>
  
  <pre><code>public class BarTabController {
      @FXML private Label lblText;
  
      public void handleButton() {
          lblText.setText("Byebye!");
      }
  }
  </code></pre>
  
  <p>BarTab's .fxml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  &lt;?import javafx.scene.layout.VBox?&gt;
  &lt;?import javafx.scene.control.Label?&gt;
  
  &lt;?import javafx.scene.control.Button?&gt;
  &lt;VBox xmlns:fx="http://www.w3.org/2001/XInclude" fx:controller="controller.BarTabController"&gt;
      &lt;Label fx:id="lblText" text="Helllo" /&gt;
      &lt;Button fx:id="btnBarTab" onAction="#handleButton" text="Change text"/&gt;
  &lt;/VBox&gt;
  </code></pre>
  
  <p>The above onAction for both FooTab and BarTab works with their respective buttons. When this method (handleButton) is references from the Main controller, that's when I get an exception.</p>