FXML Variables not binding

2020-03-30 06:48发布

问题:

I am having an issue with my FXML injection. I have setup my program as far as I can tell but it seems that I am missing something. My code is below:

Main:

package application;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;


public class Main extends Application {

    @Override
    public void start(Stage primaryStage) {

        try {

            FXMLLoader loader = new FXMLLoader(getClass().getResource("NoteKeeper.fxml"));

            BorderPane root = (BorderPane)loader.load();
            Scene scene = new Scene(root,root.getHeight(),root.getWidth());
            scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}

Controller:

package application;




import java.net.URL;
import java.util.ResourceBundle;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.layout.BorderPane;

public class NoteKeeperController implements Initializable{
    NoteBook noteBook;
    TreeItem<String> rootItem;

    public BorderPane root;

    @FXML private TreeView<String> noteTree;

    @FXML private ScrollPane sp;
    @FXML private Button newNoteButton;
    public NoteKeeperController(){

        rootItem = new TreeItem<String> ("FirstNote");
        rootItem.setExpanded(true);     
        noteTree.setRoot(rootItem);

        noteBook= new NoteBook();

    }
    @FXML
    private void closeProgram(){
        System.exit(0);
    }

    @FXML
    private void addNewNote(){
        Note note = noteBook.newNote("test", "Problem");
        TreeItem<String> newItem= new TreeItem<>(note.getNoteName());
        rootItem.getChildren().add(newItem);
    }

    public BorderPane getRoot() {
        return root;
    }

    public void setRoot(BorderPane root) {
        this.root = root;
    }

@Override
public void initialize(URL location, ResourceBundle resources) {
    // TODO Auto-generated method stub

}

}

FXML File:

<BorderPane fx:id="root" prefHeight="719.0" prefWidth="893.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="application.NoteKeeperController">
   <center>
      <BorderPane prefHeight="641.0" prefWidth="872.0" BorderPane.alignment="CENTER">
         <center>
            <TabPane prefHeight="200.0" prefWidth="200.0" tabClosingPolicy="UNAVAILABLE" BorderPane.alignment="CENTER">
              <tabs>
                <Tab text="Untitled Tab 1">
                  <content>
                    <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
                  </content>
                </Tab>
                <Tab text="Untitled Tab 2">
                  <content>
                    <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
                  </content>
                </Tab>
              </tabs>
            </TabPane>
         </center>
         <left>
            <ScrollPane fx:id="sp" prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER">
              <content>
                <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="650.0" prefWidth="200.0">
                     <children>
                        <TreeView fx:id="noteTree" prefHeight="650.0" prefWidth="200.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
                     </children>

Scenebuilder recognizes the fields that I'm injecting noteTree, sp , and newNoteButton. However when testing I am getting a NPE when I try to load items to my treeview. I confirmed using if statements that my 3 fields are null. The stack trace is below:

Caused by: java.lang.NullPointerException
    at application.NoteKeeperController.<init>(NoteKeeperController.java:31)

Also as it turns out my @FXML closeProgram() and my @FXML addNewNote() functions are being passed ok.

Can anyone point out what I am missing/doing wrong?

回答1:

You are trying to set the root item of the tree view in the controller's constructor.

When the FXMLLoader loads the fxml file, it will parse the fxml file, noting any fx:id attributes. It will instantiate the controller (by calling it's no-arg constructor), and then it will initialize any @FXML-annotated fields with the corresponding objects with matching fx:id attributes. When that is done, it calls the controller's initialize() method, if there is one.

So your constructor is executed before the noteTree is initialized by the FXMLLoader (and of course, this is the only order in which things could possibly happen). Hence when you call

    noteTree.setRoot(rootItem);

noteTree is still null.

The fix is simply to move the code in the constructor to the initialize method:

public class NoteKeeperController implements Initializable{
    NoteBook noteBook;
    TreeItem<String> rootItem;

    public BorderPane root;

    @FXML private TreeView<String> noteTree;

    @FXML private ScrollPane sp;
    @FXML private Button newNoteButton;

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

        rootItem = new TreeItem<String> ("FirstNote");
        rootItem.setExpanded(true);     
        noteTree.setRoot(rootItem);

        noteBook= new NoteBook();

    }

    // ...
}