This question already has an answer here:
This is likely pilot error, but the FXML attribute is not binding to the controller class on fx:id. I've whittled it down to a trivial example, but still "no joy". What am I overlooking?
FXML file...
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<BorderPane fx:id="mainFrame" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.controller.BorderPaneCtrl">
<left>
<AnchorPane fx:id="anchorPaneLeft" prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER" />
</left>
</BorderPane>
The associated Java code is...
package sample.controller;
import javafx.fxml.FXML;
import javafx.scene.layout.AnchorPane;
public class BorderPaneCtrl {
@FXML private AnchorPane anchorPaneLeft;
public BorderPaneCtrl() {
/* so, @FXML-annotated variables are accessible, but not
* yet populated
*/
if (anchorPaneLeft == null) {
System.out.println("anchorPaneLeft is null");
}
}
/* this is what was missing...added for "completeness"
*/
@FXML
public void initialize() {
/* anchorPaneLeft has now been populated, so it's now
* usable
*/
if (anchorPaneLeft != null) {
// do cool stuff
}
}
Ego is not an issue here, I'm pretty sure I'm overlooking something simple.
FXML elements are not assigned yet in constuctor, but you can use Initializable interface where elements are already assigned.
I assume that you know that you should create controllers with FXML by using for example:
FXMLLoader.load(getClass().getResource("sample.fxml");