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



我在场景生成器的帮助下创建了一个UI,但当我试图使用我的组件并从中添加/减去文本时,我会得到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中添加了我的控制器类,并检查了我的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");
}
}

上面的代码应该用文本"填充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之后。

最新更新