我从setText()
方法尝试在带注释的标签上调用start()
时收到以下异常。我看过一个类似的问题,但之所以对那个人不起作用的原因是因为他的标签没有注释,而我的标签是。
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$launchApplication1$162(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(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$null$148(Unknown Source)
at java.lang.Thread.run(Unknown Source)
这是我的主要课程:
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>
那么我在这里想念的是什么?
答案 0 :(得分:2)
您未在控制器上调用applyBuild(),而是在应用程序实例上调用它。 @FXML
- 带注释的字段仅在控制器中初始化。使控制器和应用程序分开类更好,以避免这种混淆。创建Application
类的两个实例也是不好的做法。
将一个单独的类写为控制器:不要使用Application
子类作为控制器类。