0

I'm new to javaFX and FXML and for a week now I'm trying to bind a collection of circles (they are some GUI indicators) to some method, and don't get it.

I did not fully understand:

  1. Where should I initialize my circles?
  2. How to make my GUI update after the first Main.stage.show?
  3. Should I use animation for that?

To be more specific my intention is to see some animation, that means seeing the GUI in its initial state and then after a few seconds of delay I wish to see the first circle painted red for example. Thanks!

My current output is that my GUI updates instantly and I also get the following error:

Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
    at sample.UIController.lambda$changeCircleColor$0(UIController.java:87)
    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)
    at java.lang.Thread.run(Thread.java:748)
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
    at sample.UIController.lambda$changeCircleColor$1(UIController.java:93)
    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)
    at java.lang.Thread.run(Thread.java:748)
יונ 09, 2019 9:06:39 PM javafx.fxml.FXMLLoader$ValueElement processValue
WARNING: Loading FXML document with JavaFX API of version 11.0.1 by JavaFX runtime of version 8.0.191
package sample;

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

import java.io.IOException;
import java.util.ArrayList;


public class Main extends Application {
    public static Stage stage = new Stage();
    public static Parent root = new Parent() {
    };

    /**
     * the main method of the program
     */
    public static void main(String args[]) {

        UIController uiController = new UIController();
        uiController.changeCircleColor();

        Application.launch(Main.class, args);
    }

    public void start(Stage stage){
        this.stage = stage;
        FXMLLoader loader = new FXMLLoader();
        try {
            loader.setLocation(getClass().getResource("resources/UserInterface.fxml"));
            this.root = loader.load();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e){
            e.printStackTrace();
        }
        stage.setTitle("Troubleshooting project");
        stage.setScene(new Scene(root, 900, 700));
        stage.show();
    }

}
fabian
  • 80,457
  • 12
  • 86
  • 114
Limor Halevi
  • 29
  • 1
  • 6
  • As for the current error you get: https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it As for the rest: That's a very broad question. We won't be able to answer this without you adding more details about the input/desired output. – fabian Jun 09 '19 at 20:26
  • You're creating an instance of `UIController` and invoking a method on it before the JavaFX runtime is even launched, let alone any FXML being loaded. Also, the `FXMLLoader` will create its own instance of the controller if you've specified the appropriate `fx:controller` attribute—you'll need to use that instance (that's the one that's had any UI objects injected). – Slaw Jun 10 '19 at 05:44

0 Answers0