从内部类访问时,应该由fxml分配的字段抛出NullPointerException-javaFX

时间:2018-09-04 14:47:58

标签: java javafx fxml inner-classes

我有一个用于fxml文件的控制器。控制器具有一个字段public BorderPane mainBorderPane;,该字段应该由fxml文件中找到的同一fx:id的“边框”窗格填充。当我尝试从内部类访问它时,它会给出一个NullPointerExcetion

clearBorderPaneCenter();clearBorderPaneRight正常工作,但是当我运行cashSceneController.show()时,它在行mainBorderPane.setRight(rightLayout);上崩溃了

fxml文件:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.image.*?>
<?import java.net.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.text.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<BorderPane fx:id="mainBorderPane" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Program.gui.MainSceneController">
   <top>
      <Pane prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER" />
   </top>
   <left>
      <Pane prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER" />
   </left>
   <center>
      <Pane prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER" />
   </center>
   <right>
      <Pane prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER" />
   </right>
   <bottom>
      <Pane prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER" />
   </bottom>
</BorderPane>

控制器的简化版本:

public class MainSceneController {

    MainSceneController.CashSceneController cashSceneController = new MainSceneController.CashSceneController();
    public BorderPane mainBorderPane;

    public void switchLayout(byte ID){
        //todo Make this work switchScene()

        clearBorderPaneCenter();
        clearBorderPaneRight();
        cashSceneController.show();
    }

    public void clearBorderPaneRight(){
        try {
            mainBorderPane.setRight(null);
            OutputHelper.log("cleared right of mainBorderPane");
        } catch (Exception e){
            OutputHelper.log("clearing right of mainBorderPane not required - already cleared");
        }
    }

    public void clearBorderPaneCenter(){
        try {
            mainBorderPane.setCenter(null);
            OutputHelper.log("cleared centre of mainBorderPane");
        } catch (Exception e){
            OutputHelper.log("clearing centre of mainBorderPane not required - already cleared");
        }
    }

    public class CashSceneController{
        VBox rightLayout;
        public void show() {
            setVBox();
            mainBorderPane.setRight(rightLayout);
        }

       public void setVBox(){
           rightLayout = new VBox();
           //......
       }
    }
}

这是fxml文件的加载方式

SceneController = new MainSceneController(new Scene(FXMLLoader.load(getClass().getResource("gui/MainScreen.fxml"))));

我希望我已经很好地解释了我的问题。我是堆栈溢出的新手,也不知道什么是好问题

编辑:看来问题是由根本没有分配mainBorderPane引起的。 clearBorderPaneCenterclearBorderPaneRight似乎没有崩溃,因为它们被try catch捕获了。为什么mainBorderPane可能未正确分配的任何想法?

1 个答案:

答案 0 :(得分:0)

您缺少fxml批注。 FXMLLoader需要将BorderPane注入代码中

    @FXML      
    public BorderPane mainBorderPane;