Scenebuilder JavaFX 组件无法正确连接我的控制器 class

Scenebuilder JavaFX components does not correctly connect with my controller class

我在 scenebuilder 的帮助下创建了一个 UI,但是当我尝试使用我的组件和来自它们的 add/subtract 文本时,我得到了 NPE。

当我尝试 运行 我的简单代码时遇到的错误:

Cannot invoke "javafx.scene.control.TextArea.setText(String)" because"this.quoteOfTheDay" is null
at controller.Controller.fillTextAreaWithWebsites(Controller.java:36)
at controller.Main.updateQuotePanel(Main.java:41)
at controller.Main.start(Main.java:22)

我已经在 scenebuilder 中添加了我的控制器 class 并且检查了我的 fxid 的拼写一百万次。

如果需要,我会很乐意添加更多代码和说明。 包控制器;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import java.io.IOException;
import java.util.Objects;

public class Main extends Application {

@Override
public void start(Stage primaryStage) throws IOException {
    //Path to FXML file
    FXMLLoader fxmlLoader = new FXMLLoader(Main.class.getResource("/FXML/mainPage.fxml"));

    Parent root = fxmlLoader.load();
    updateQuotePanel();
    Scene scene = new Scene(root, 613, 702);
    primaryStage.setTitle("Test");
    primaryStage.setResizable(false);
    primaryStage.setScene(scene);
    primaryStage.show();
}

public static void main(String[] args) {
    launch();
}
public void updateQuotePanel() {
    Webscraping webscraping = new Webscraping();
    Controller controller = new Controller();
    controller.fillTextAreaWithQuotes();
}

}

A simplified example of how my code is structured

public class Controller implements Initializable {
@FXML
private TextArea quoteOfTheDay;

  @FXML
  public void fillTextAreaWithQuotes() {
      quoteOfTheDay.setText("Quotes");
  }
}

上面的代码应该用文本“Quotes”填充 SceneBuilder 中名为 TextArea 的组件,但它没有,而是在调用 TextArea 时给了我一个 NPE。

这是我的 FXML 文件:

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.Pane?>

<AnchorPane fx:id="mainPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="702.0" prefWidth="612.0" style="-fx-background-color: #C996CC; -fx-background-radius: 1em; -fx-background-color: #476072; -fx-border-radius: 1em;" xmlns="http://javafx.com/javafx/16" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.Controller">
   <children>
      <AnchorPane layoutY="100.0" prefHeight="4.0" prefWidth="613.0" style="-fx-background-color: transparent;" />
      <AnchorPane fx:id="menuBar" layoutX="-1.0" prefHeight="112.0" prefWidth="613.0" style="-fx-background-color: #398AB9; -fx-background-radius: 1em;">
         <children>
            <Button fx:id="removeWebsiteBtn" layoutX="330.0" layoutY="25.0" mnemonicParsing="false" onAction="#removeWebsiteScene" style="-fx-background-color: #398AB9;">
               <graphic>
                  <ImageView fitHeight="50.0" fitWidth="51.0">
                     <image>
                        <Image url="@../icons/128trashcanIcon.png" />
                     </image>
                  </ImageView>
               </graphic>
            </Button>
            <Button fx:id="settingsPageBtn" layoutX="70.0" layoutY="25.0" mnemonicParsing="false" onAction="#settingsPageScene" onMouseClicked="#removeWebsiteScene" style="-fx-background-color: #398AB9;">
               <graphic>
                  <ImageView fitHeight="50.0" fitWidth="51.0">
                     <image>
                        <Image url="@../icons/128settingsIcon.png" />
                     </image>
                  </ImageView>
               </graphic>
            </Button>
            <Button fx:id="findFileBtn" layoutX="200.0" layoutY="25.0" mnemonicParsing="false" onAction="#findFileScene" style="-fx-background-color: #398AB9;">
               <graphic>
                  <ImageView fitHeight="50.0" fitWidth="51.0">
                     <image>
                        <Image url="@../icons/128profilepicpng.png" />
                     </image>
                  </ImageView>
               </graphic>
            </Button>
            <Button fx:id="addWebsiteBtn" layoutX="460.0" layoutY="25.0" mnemonicParsing="false" onAction="#addWebsiteScene" style="-fx-background-color: #398AB9;">
               <graphic>
                  <ImageView fitHeight="50.0" fitWidth="51.0" onMouseClicked="#addWebsiteScene">
                     <image>
                        <Image url="@../icons/128additionIcon.png" />
                     </image>
                  </ImageView>
               </graphic>
            </Button>
            <Button layoutX="585.0" layoutY="2.0" mnemonicParsing="false" onAction="#exitApplication" style="-fx-background-color: #398AB9;" text="X" />
            <Button layoutX="559.0" layoutY="-5.0" mnemonicParsing="false" onAction="#setMinimized" style="-fx-background-color: #398AB9;" text="_" />
         </children>
      </AnchorPane>
      <Pane layoutX="11.0" layoutY="174.0" prefHeight="216.0" prefWidth="308.0" style="-fx-background-radius: 1em; -fx-background-color: white;">
         <children>
            <TextArea layoutX="4.0" layoutY="4.0" prefHeight="210.0" prefWidth="301.0" style="-fx-border-width: 0;" />
         </children>
      </Pane>
      <Label layoutX="25.0" layoutY="123.0" prefHeight="42.0" prefWidth="280.0" style="-fx-font-size: 24; -fx-text-fill: white;" text="Popular websites 2021:" />
      <Pane layoutX="14.0" layoutY="438.0" prefHeight="216.0" prefWidth="308.0" style="-fx-background-radius: 1em; -fx-background-color: white;">
         <children>
            <TextArea fx:id="quoteOfTheDay" layoutX="4.0" layoutY="4.0" prefHeight="210.0" prefWidth="301.0" style="-fx-border-width: 0;" />
         </children>
      </Pane>
      <Label layoutX="25.0" layoutY="390.0" prefHeight="42.0" prefWidth="319.0" style="-fx-font-size: 24; -fx-text-fill: white;" text="Motivational quote for today:" />
   </children>
</AnchorPane>

您需要使用加载的控制器,而不是创建一个新控制器。

不要写:

Controller controller = new Controller();

写入:

Controller controller = fxmlLoader.getController();

加载 fxml 后。