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

标签 java javafx fxml inner-classes

我有一个 fxml 文件的 Controller 。 Controller 有一个字段 public BorderPane mainBorderPane;它应该填充相同的边框 Pane fx:id在 fxml 文件中找到。当我尝试从内部类访问它时,它给出 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>

简化版 Controller :

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 可能无法正确分配吗?

最佳答案

您缺少 fxml 注释。需要FXMLLoader将BorderPane注入(inject)到代码中

    @FXML      
    public BorderPane mainBorderPane;

关于java - 从内部类访问时,应该由 fxml 分配的字段抛出 NullPointerException - javaFX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52168913/

相关文章:

java - 在我的程序中引用另一个 .java 文件中的类

java - Java 中的 Desktop.getDesktop.open() 失败

JavaFX:LoadException 解析按钮事件处理程序的 onAction

JavaFX : Integrating Spring framework with JavaFX app(Incorrect configuration)

java - 尝试在 Text 对象上使用 .setText 时出现 NullPointerException

Java NIO SSL - SSLEngine session 重用

java - 在 Eclipse 插件中使用 Xalan

javafx 在适当的 Controller 类中使用 MainController 或其他 Controller 中的对象

java - 常规变量的 ObservableList 概念(无 TableView)

java - 我的代码是否过于程序化?