-2

Edit: Static's removed. Still getting the same error

I've created a GUI with a Listview in Scene builder and now I'm trying to populate the Listview with some Strings.

I'm using 2 classes at the moment. Here are 2 cut down versions of the scripts for easier reading MainController:

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;


public class MainController extends Application
{
    @Override
    public void start(Stage primaryStage) throws Exception
    {
        Parent root = FXMLLoader.load(getClass().getResource("IntelligentSystems.fxml"));
        primaryStage.setTitle("Intelligent Systems Agent Program");
        primaryStage.setScene(new Scene (root));
        primaryStage.show();

        GUIController GUIList = new GUIController();
        GUIList.DoList.add("agentCtrl");
        GUIList.DoList.add("DeliveryAgent");
        GUIList.PopulateAgentList();
    }

    public static void main (String[] args)
    {
        launch(args);
    }
}

and the second Class, GUIController:

import javafx.fxml.FXML;
import javafx.scene.control.ListView;
import javafx.collections.ObservableList;
import javafx.collections.FXCollections;

public class GUIController {
    //List View variables.
    @FXML
    public ListView AgentsList;

    @FXML
    public ObservableList<String> DoList= FXCollections.observableArrayList();

    @FXML
    public void PopulateAgentList()
    {
        AgentsList.setItems(DoList);
    }
}

Currently, I've set the fxId of the Listview to "AgentsList" in the fxml file.

<ListView fx:id="AgentsList" layoutX="15.0" layoutY="39.0" prefHeight="200.0" prefWidth="86.0" />

Whenever I run the program in IntelliJ, it always returns a java.lang.NullPointerException around the "AgentsList.setItems(_doList);" line.

I'm completely lost as to why it can't add to the list. Not helped that I haven't been using Java for long as I've mostly worked with C#.

Edit: Exception Stack Trace:

java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
    at java.base/java.lang.Thread.run(Thread.java:830)
Caused by: java.lang.NullPointerException
    at GUIController.PopulateAgentList(GUIController.java:26)
    at MainController.start(MainController.java:57)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
    ... 1 more
Exception running application MainController

Process finished with exit code 1
Cfusion
  • 19
  • 6
  • It would be helpful if you posted the entire exception stack trace. – Kaan Oct 01 '19 at 03:25
  • One moment, I'll add it now – Cfusion Oct 01 '19 at 03:27
  • 1
    Why are you making everything static? I don't think `@FXML` will inject statics. (And you're following C# rather than Java naming conventions all over the place.) – chrylis -cautiouslyoptimistic- Oct 01 '19 at 03:44
  • Was following IntellIJ's fix recommendation. Was trying to get it to reference the listview from another class and it says a non static field can't be referenced from a static context. Honestly, want to avoid using so many statics but IntelliJ removed the error messages when the static's were added. – Cfusion Oct 01 '19 at 03:54

1 Answers1

3

Your @FXML annotated fields are static, which is not supported by JavaFX, as it is desinged to create/load multiple instances of the same fxml file/controller class. If you are unfamiliar with what 'static' actually means, you can refer to What is the difference between a static method and a non-static method?.

As for how you get the correct controller instance, you need to load the fxml file a little bit different.

FXMLLoader loader = new FXMLLoader(getClass().getResource("IntelligentSystems.fxml"));
Parent root = loader.load(); // must be called before getting the controller!
GUIController controller = loader.getController();

Now you can use the controller variable to access the fields in a non-static way.

n247s
  • 1,898
  • 1
  • 12
  • 30
  • That worked. Thank you so much. Will look at that link as well to familiarise myself with the differences. – Cfusion Oct 01 '19 at 04:28