我对使用FXML作为JavaFX的源代码很陌生。不知道我做错了什么。字段显然为空,但是如果我尝试初始化它们,它们不会使用与FXML文件中的对象不同的对象吗?
布局简单:
控制器类仅包含此代码。我只是在测试并尝试将项目添加到列表视图,但显然拍卖列表视图为空并且没有指向任何内容。当我尝试使用它时,gridPane也是如此。
控制器类:
public class Controller {
@FXML
private GridPane gridPane;
@FXML
private ListView<String> auctionListView;
public void init() {
ObservableList<String> ol = FXCollections.observableArrayList("Hi",
"Hello", "Hellooo");
if (auctionListView == null)
System.out.println("Null.");
auctionListView.setItems(ol); // <-----Error occurs at this line.
}
}
对于 Main 类来说没什么好说的,只是常规 JavaFX 应用程序的通常简单启动
主要:
public static void main(String[] args){
launch(args); //This launches the JavaFX GUI.
}
@Override
public void start(Stage primaryStage) throws Exception {
Controller controller = new Controller();
Parent fxml = FXMLLoader.load(Main.class.getClassLoader().getResource("scene.fxml"));
Scene scene = new Scene(fxml);
primaryStage.setScene(scene);
primaryStage.setResizable(false);
primaryStage.setTitle("Interest Viewer");
primaryStage.show();
controller.init();
}
Scene.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.effect.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.media.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.shape.*?>
<?import javafx.scene.image.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane prefHeight="600.0" prefWidth="800.0" stylesheets="@style.css" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Controller">
<children>
<BorderPane prefHeight="600.0" prefWidth="800.0">
<left>
<VBox prefHeight="600.0" prefWidth="205.0" BorderPane.alignment="CENTER">
<children>
<ListView fx:id="auctionListView" fixedCellSize="50.0" nodeOrientation="LEFT_TO_RIGHT" stylesheets="@list.css" VBox.vgrow="ALWAYS">
<VBox.margin>
<Insets />
</VBox.margin>
<padding>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</padding>
</ListView>
</children>
</VBox>
</left>
<center>
<GridPane fx:id="gridPane" BorderPane.alignment="CENTER">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Button mnemonicParsing="false" text="Button" />
</children>
</GridPane>
</center>
</BorderPane>
</children>
</AnchorPane>
请注意"Null.",因为它是从控制器类中拍卖列表视图的简单空检查中打印出来的。
输出/错误:
Null.
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NullPointerException
at Controller.init(Controller.java:19)
at Main.start(Main.java:26)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
... 1 more
Exception running application Main
您的init()
方法是问题所在。为了初始化注入 FXML 的控件,您需要将其更改为包含 @FXML
标记:
@FXML
private void initialize() {
// Your code goes here
}