具有构造函数的JavaFX嵌套控制器,导致“ NullPointerException:根不能为null”

时间:2019-01-29 22:01:35

标签: javafx openjfx

制作javafx11 fxml gui。我有一个主要的fxml“ testingScreen”和“ BinaryIndi​​cator”,它们分别包含在“ TestingScreen”中以及它们各自的控制器。 BinaryIndi​​cator的控制器需要一些需要构造函数的数据。在我将fx:id添加到TestingScreen内包含的BinaryIndi​​cator之前,此方法可以正常工作。因此,它在没有BinaryIndi​​cator的fx:id的情况下都可以工作,但是这不允许我从TestingScreen的Controller中对其进行控制,而我需要构造函数。然后我的Gradle运行失败,回复了

 "Caused by: java.lang.NullPointerException: Root cannot be null
    at javafx.graphics/javafx.scene.Scene.<init>(Scene.java:345)
    at javafx.graphics/javafx.scene.Scene.<init>(Scene.java:236)
    at com.team871.FXMLGraphicalMain.start(FXMLGraphicalMain.java:32)"

我的主:

public class FXMLGraphicalMain extends Application {


    @Override
    public void start(Stage primaryStage) {
        Parent root = null;

        try {
            root = FXMLLoader.load(getClass().getClassLoader().getResource("FXML/TestScreen.fxml"));
        } catch (IOException e) {
            System.out.println("Failed to load the FXML file!");
        }

        primaryStage.setTitle("Test 1");
        primaryStage.setScene(new Scene(root, 720, 480));
        primaryStage.show();
    }


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

我的主要TestingScreen FXML文档:

 <AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.172-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.team871.controllers.TestScreen">
   <children>
      <Button fx:id="button" layoutX="71.0" layoutY="21.0" mnemonicParsing="false" onAction="#handleButtonClick" text="Hello?" />
      <fx:include fx:id="binaryIndicator" source="/FXML/modules/BinaryIndicator.fxml"/>
   </children>
</AnchorPane>

TestingScreen的控制器:

@FXML public Button button;

    @FXML public BinaryIndicator binaryIndicator;

    public TestScreen(){
        binaryIndicator = new BinaryIndicator();
    }


    @FXML private void initialize() {
    }

    public void handleButtonClick(){
        System.out.println("hello");
    }

BinaryIndi​​cator FXML:

<VBox alignment="CENTER" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="76.0" prefWidth="57.0" xmlns="http://javafx.com/javafx/8.0.172-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.team871.controllers.modules.BinaryIndicator">
   <children>
      <Label fx:id="label" text="Title" />
      <Circle fx:id="circle" fill="#fff01f" radius="25.0" stroke="BLACK" strokeType="INSIDE" />
      <Region prefHeight="26.0" prefWidth="57.0" />
   </children>
</VBox>

BinaryIndi​​cator的控制器:

public BinaryIndicator(){
    this(new ColorMode(true), new BinaryDataValue(false), "null");
}

public BinaryIndicator(ColorMode colorMode, IData<Boolean> data, String title) {
    this.colorMode = colorMode;
    this.title = title;
    this.data = data;


    label = new Label(this.title + ":");
    label.setTextFill(colorMode.getSecondaryColor());
    label.setAlignment(Pos.CENTER);
    label.setPadding(new Insets(5, 0, 3, 0));

    circle = new Circle();
    setNeutral();


    //Updates:
    colorMode.addListener(observable -> {
        label.setTextFill(colorMode.getSecondaryColor());
    });

    data.addListener((observable, old, newValue) -> {
        setState(newValue);
    });
}

0 个答案:

没有答案