I'm coding a Scene that represents the main menu of my JavaFX program;
This Scene also uses a background working Thread that every 1-2 seconds spawns a Shape and makes it translate from left to right using a TranslateTransition, and removes it when the animation is complete;
My problem is that "rarely" this Exception is thrown, but I can't undestand why because it doesn't seem to be a problem in one of my Class:
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at com.sun.scenario.animation.AbstractMasterTimer.timePulseImpl(AbstractMasterTimer.java:344)
at com.sun.scenario.animation.AbstractMasterTimer$MainLoop.run(AbstractMasterTimer.java:267)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:506)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:490)
at com.sun.javafx.tk.quantum.QuantumToolkit.lambda$runToolkit$404(QuantumToolkit.java:319)
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$148(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
...
Continuously printing this until I close the program.
Does someone have an idea?
EDIT: Here's the code, I had to reduce it from 1380 lines to around 100, I hope it was worth the time ahah!
import javafx.animation.TranslateTransition;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Shape;
import javafx.stage.Stage;
import javafx.util.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.CountDownLatch;
import static ExceptionMain.Status.NEW;
public class ExceptionMain extends Application {
public enum Status {NEW, RUNNING, FINISHED}
public static void main(String[] args) {launch(args);}
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setScene(new Main_Menu().menu_Scene);
primaryStage.show();
}
class Main_Menu {
Main_Menu() {
panel = new Pane();
ram_List = new ArrayList<>();
indexes = new ArrayList<>();
wait_Time = -1;
menu_Scene = new Scene(panel, 720, 250);
Thread thread = new Thread(() -> {
try {
while (!Thread.currentThread().isInterrupted()) {
ram_List.add(new Random_Animation_Manager());
for (int i = ram_List.size() - 1; i >= 0; i--) {
if (ram_List.get(i).status == NEW) {
try {
ram_List.get(i).run();
wait_Time = ram_List.get(i).wt;
} catch (InterruptedException ignored) {}
} else if (ram_List.get(i).status == Status.FINISHED)
indexes.add((short) i);
}
indexes.forEach(index -> ram_List.remove(ram_List.get(index)));
indexes.clear();
Thread.sleep(wait_Time);
}
} catch (InterruptedException ignored) {}
});
thread.setDaemon(true);
thread.start();
}
Scene menu_Scene;
Pane panel;
List<Random_Animation_Manager> ram_List;
List<Short> indexes;
short wait_Time;
class Random_Animation_Manager {
Random_Animation_Manager() {status = NEW;}
TranslateTransition tt;
Shape shape;
short wt;
volatile Status status;
void run() throws InterruptedException {
status = Status.RUNNING;
Random ram = new Random();
wt = (short) (1 + ram.nextInt(5));
shape = new Circle(10);
shape.setLayoutX(-150);
shape.setLayoutY((short) (1 + ram.nextInt(250)));
shape.setFill(Color.ORANGE);
tt = new TranslateTransition(Duration.millis(4000), shape);
tt.setToX(920);
CountDownLatch wait_For_Platform_Operation = new CountDownLatch(1);
Platform.runLater(() -> {
panel.getChildren().add(shape);
wait_For_Platform_Operation.countDown();
});
wait_For_Platform_Operation.await();
tt.setOnFinished(event -> {
panel.getChildren().remove(shape);
status = Status.FINISHED;
});
tt.play();
}
}
}
}
You can imagine my real code is very different, I simplified a lot of things trying to be left with the main Characters!
As you can see, my background thread creates a Random_Animation_Manager that puts in a list;
He then checks for every elements of the list, and if it's in a NEW state, it runs the object that spawns the Shape, if it's in the FINISH state, it stores the index of the Object to be deleted; In the ends, it waits for a while (the time value is randomly created in the Object and passed to the thread) and the loop continues.
I hope someone can tell me what's the problem, I even speeded up the "deployment time" so you should get the error faster!
I'm still "new" to JavaFX programming and concurrency, I hope I can learn something from this!