JavaFX @FXML注入的字段为null?

时间:2019-01-01 22:42:42

标签: java javafx null fxml

我对使用FXML作为JavaFX的来源还很陌生。不知道我做错了什么。 字段显然为空,但是如果我尝试对其进行初始化,它们是否会使用与FXML文件中的对象不同的对象?

简单的布局:

Controller类仅包含此代码。我只是在测试并尝试将项目添加到ListView中,但显然AuctionListView为null,并且未指向任何内容。尝试使用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>

注意“空”。因为它是从Controller类中的auctionListView的简单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

1 个答案:

答案 0 :(得分:0)

您的init()方法是问题所在。为了初始化用FXML注入的控件,您需要对其进行更改以包含@FXML标签:

@FXML
private void initialize() {
    // Your code goes here
}