2

I'm pretty new to using FXML as a source for JavaFX. Not sure what I did wrong. The Fields are apparently null, but if I'd try to initialize them wouldn't they be using a different object than the ones from the FXML file?

Simple layout:

The Controller class just contains this code. I'm just testing and trying to add items to the ListView but apparently the auctionListView is null and not pointing to anything. Same goes for gridPane when I try to use it.

Controller class:

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.
    }
}

Nothing too much to say for the Main class, just the usual simple startup for a regular JavaFX application

Main:

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>

Note the "Null." as that was printed out from the simple null check for the auctionListView in the Controller class.

Output/Error:

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
Artish1
  • 113
  • 1
  • 12
  • You're not using `init` for the controller instance that is used with the fxml (the one created by `FXMLLoader`), but an instance that you create yourself. – fabian Jan 01 '19 at 22:48

1 Answers1

0

Your init() method is the problem. In order to initialize the controls injected with FXML, you need to change that to include the @FXML tag:

@FXML
private void initialize() {
    // Your code goes here
}
Zephyr
  • 9,885
  • 4
  • 28
  • 63