0

I'm making a JavaFX application but for some reason, my controller class isn't recognizing the FX ids in my FXML file.

Here is my FXML code:

//fxml code....
<AnchorPane id="Meta Data GUI" maxHeight="-1.0" maxWidth="-1.0" minHeight="-1.0" minWidth="-1.0" prefHeight="-1.0" prefWidth="-1.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="com.cisco.gaxl.metadatagui.windows.controllers.MainWindowController">
   //components....
   <Button fx:id="addTableButton" onAction="#testMethod" contentDisplay="CENTER" mnemonicParsing="false" prefWidth="-1.0" text="Add Table" textAlignment="CENTER" VBox.vgrow="NEVER">
   //more components...
</AnchorPane>

And here is my Java code

import javafx.fxml.FXML
//more imports....
public class MainWindowController {
     //fields....
     @FXML private static addTableButton;

     @FXML void initialize() {
         //code...
         assert addTableButton != null : "fx:id=\"addTableButton\" was not injected: check your FXML file 'metadataEditor.fxml'.";
         //code....
         addTableButton.setOnAction(new EventHandler<ActionEvent>() {
             //code...
         }
     }

     @FXML private void testMethod() { System.out.println("Something"); }

}

I get a nullpointer exception at the addTableButton.setOnAction declaration, whcih is weird because it should've been caught by my assert statement. I have verified that my class is correctly defined as the controller class and that all other parts of my FXML are correct.

Now if I comment out the setOnAction declaration (as well as all my other JavaFX components) the GUI will output as normal but just without any actions. However, clicking the addTableButton will still execute testMethod as I defined it in my FXML file, so it seems like everything but the fx:id's are working.

Does anyone have an idea as to what's going on here? I'm sure I'm doing something very silly but any help is appreciated!

Kevino Sun
  • 62
  • 1
  • 9

1 Answers1

0

Javafx doesn't seem to accept static variables, also you didn't declare a type. Here's what you would need:

@FXML
private Button addTableButton;