我正在编写代表我的JavaFX程序主菜单的场景;
此场景还使用背景工作线程,每1-2秒生成一个Shape并使用TranslateTransition从左向右翻译,并在动画完成时将其删除;
我的问题是"很少"这个例外被抛出,但我无法理解为什么,因为它似乎不是我班级中的一个问题:
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.
有人有想法吗?
编辑: 这是代码,我不得不将它从1380行减少到100左右,我希望它值得花时间啊!
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();
}
}
}
}
你可以想象我的真实代码是非常不同的,我简化了许多试图留下主要角色的东西!
如您所见,我的后台线程创建了一个放入列表的Random_Animation_Manager;
然后,他检查列表中的每个元素,如果它处于 NEW 状态,它会运行生成Shape的对象,如果它在<< strong> FINISH 状态,它存储要删除的Object的索引;最后,它等待一段时间(时间值在Object中随机创建并传递给线程)并继续循环。我希望有人可以告诉我这是什么问题,我甚至加快了部署时间&#34;所以你应该更快地得到错误!
我还是&#34;新&#34;对于JavaFX编程和并发,我希望我能从中学到一些东西!