简体   繁体   English

简单的Javafx TreeView引发Nullpointer异常

[英]Simple Javafx TreeView throws Nullpointer Exception

I've got the following classes: 我有以下课程:

Main: 主要:

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{

        BorderPane root = FXMLLoader.load(getClass().getResource("../view/PersonOverview.fxml"));

        AnchorPane view2 = FXMLLoader.load(getClass().getResource("../view/view2.fxml"));
        root.setLeft(view2);

        primaryStage.setScene(new Scene(root, 1000, 600));
        primaryStage.show();
    }

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

TreeController: TreeController:

public class TreeController implements Initializable {

     //Set icon for folder
     Node folderIcon = new ImageView(new Image(this.getClass().getResourceAsStream("../icon/icon.jpg")));

     //Set root
     TreeItem<String> root; 

     @FXML TreeView<String> tree;

     //Set other Items
     private TreeItem<String> item1 = new TreeItem<String>("item1", folderIcon);
     private TreeItem<String> item2 = new TreeItem<String>("item2", folderIcon);
     private TreeItem<String> item3 = new TreeItem<String>("item3", folderIcon);
     private TreeItem<String> item4 = new TreeItem<String>("item4", folderIcon);
     private TreeItem<String> item5 = new TreeItem<String>("item5", folderIcon);

     //Add Children to root
     private void makeChildren() {
         root.getChildren().add(item1);
         root.getChildren().add(item2);
         root.getChildren().add(item3);
         root.getChildren().add(item4);
         root.getChildren().add(item5);
     }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        root = new TreeItem<String>("root", folderIcon);
        makeChildren();
        root.setExpanded(true);
        tree.setRoot(root);
    } 
}

And of course my view2 fxml file: 当然还有我的view2 fxml文件:

    <AnchorPane
        maxHeight="-Infinity" maxWidth="-Infinity"
        minHeight="-Infinity" minWidth="-Infinity"
        prefHeight="400.0" prefWidth="354.0"
        xmlns="http://javafx.com/javafx/8.0.40"
        xmlns:fx="http://javafx.com/fxml/1"
        fx:controller="control.TreeController">
      <children>
      <TreeView
        layoutX="69.0" layoutY="118.0" 
        prefHeight="400.0" prefWidth="354.0" 
        AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" 
        AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
      </children>
   </AnchorPane>

Now the problem I have is that it will throw a Nullpointer Exception at tree.setRoot(root); 现在我遇到的问题是它将在tree.setRoot(root);处引发Nullpointer异常tree.setRoot(root);

And a ConstructLoad Exception in my Main at: 在我的主目录中有一个ConstructLoad异常:

AnchorPane view2 = FXMLLoader.load(getClass().getResource("../view/view2.fxml"));

I'm still learning this stuff but I was told that when using FXML, you don't need to initialize TreeViews using "new" as the @FXML annotation will already take care of this with tree.setRoot(root). 我仍然在学习这些东西,但是有人告诉我,使用FXML时,您不需要使用“ new”来初始化TreeViews,因为@FXML批注已经可以通过tree.setRoot(root)来解决。

Sorry for such a noobish question but I've been googling for the past 2 hours and haven't gotten any wiser. 抱歉,您遇到这样一个愚蠢的问题,但我在过去2个小时中一直在搜索Google,并且没有任何明智的选择。

I'm still learning this stuff but I was told that when using FXML, you don't need to initialize TreeViews using "new" as the @FXML annotation will already take care of this with tree.setRoot(root). 我仍然在学习这些东西,但是有人告诉我,使用FXML时,您不需要使用“ new”来初始化TreeViews,因为@FXML批注已经可以通过tree.setRoot(root)来解决。

You guessed right, but in order for JavaFX to inject your Treeview (= make the "new" for you) you need to declare something like: 您猜对了,但是为了让JavaFX 注入 Treeview(=为您创建“ new”),您需要声明以下内容:

<Treeview fx:id="tree" />

in view2.fxml. 在view2.fxml中。

With the fx:id attribute setted to the same name as your Treeview variable in Java code. fx:id属性设置为与Java代码中的Treeview变量相同的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM