setText() 上的 NullPointer 异常,而所有内容都已正确加载和注释

NullPointer Exception on setText() while everything is properly loaded and annotated

我在尝试从 start() 方法调用带注释的标签上的 setText() 时收到以下异常。我看过一个类似的问题,但是它对那个人不起作用的原因是因为他的标签没有注释,而我的是。

java.lang.NullPointerException
at io.github.blubdalegend.openbravery.OpenBravery.applyBuild(OpenBravery.java:67)
at io.github.blubdalegend.openbravery.OpenBravery.start(OpenBravery.java:58)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication12(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait5(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null3(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater4(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null8(Unknown Source)
at java.lang.Thread.run(Unknown Source)

这是我的主class:

public class OpenBravery extends Application implements Initializable {

    @FXML
    private Button rerollButton;

    @FXML
    private Label champL;

    public static void main(String[] args) {
        System.out.println("Downloading files from Dropbox...");
        updateFiles();
        System.out.println("Download complete!");
        Application.launch(OpenBravery.class, (java.lang.String[]) null);

    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        rerollButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {

            }
        });
    }

    private static void updateFiles() {
        FileManager fm = new FileManager();
        fm.downloadChamps();
        fm.downloadItems();
    }

    @Override
    public void start(Stage stage) {
        try {
            Pane pane = (Pane) FXMLLoader.load(OpenBravery.class.getResource("build.fxml"));
            Scene scene = new Scene(pane);
            stage.setScene(scene);
            stage.setTitle("OpenBravery");
            stage.setResizable(false);
            applyBuild();
            stage.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void applyBuild() {
        Build build = new Build();
        champL.setText(build.getChamp());
    }

}

我的 build.fxml 是这样开始的:

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

<?import javafx.scene.text.*?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>


<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity"
    minWidth="-Infinity" prefHeight="297.0" prefWidth="362.0"
    xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"
    fx:controller="io.github.blubdalegend.openbravery.OpenBravery">
    <children>
        <Label layoutX="14.0" layoutY="14.0" text="Your build:">
            <font>
                <Font size="32.0" />
            </font>
        </Label>
        <Label fx:id="champL" layoutX="14.0" layoutY="61.0" prefHeight="32.0"
            prefWidth="288.0" text="Label">
            <font>
                <Font size="17.0" />
            </font>
        </Label>

那么我错过了什么?

您不是在控制器上调用 applyBuild(),而是在应用程序实例上调用它。 @FXML-注释字段仅在控制器中初始化。最好让控制器和应用程序分开 classes,以避免这种混淆。创建 Application class 的两个实例也是不好的做法。

写一个单独的class作为控制器:不要使用Application子class作为控制器class。