I'm trying to create a simple game but I'm already failing with the Background. My code works only for a couple of seconds before I'm getting this error:
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at javafx.scene.Parent.updateCachedBounds(Parent.java:1592)
at javafx.scene.Parent.recomputeBounds(Parent.java:1535)
at javafx.scene.Parent.impl_computeGeomBounds(Parent.java:1388)
at javafx.scene.layout.Region.impl_computeGeomBounds(Region.java:3078)
at javafx.scene.Node.updateGeomBounds(Node.java:3579)
at javafx.scene.Node.getGeomBounds(Node.java:3532)
at javafx.scene.Node.getLocalBounds(Node.java:3480)
at javafx.scene.Node.updateTxBounds(Node.java:3643)
at javafx.scene.Node.getTransformedBounds(Node.java:3426)
at javafx.scene.Node.updateBounds(Node.java:559)
at javafx.scene.Parent.updateBounds(Parent.java:1719)
at javafx.scene.Parent.updateBounds(Parent.java:1717)
at javafx.scene.Scene$ScenePulseListener.pulse(Scene.java:2404)
at com.sun.javafx.tk.Toolkit.lambda$runPulse$29(Toolkit.java:398)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.Toolkit.runPulse(Toolkit.java:397)
at com.sun.javafx.tk.Toolkit.firePulse(Toolkit.java:424)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:510)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:490)
at com.sun.javafx.tk.quantum.QuantumToolkit.lambda$runToolkit$403(QuantumToolkit.java:319)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at com.sun.glass.ui.gtk.GtkApplication.lambda$null$48(GtkApplication.java:139)
at java.lang.Thread.run(Thread.java:748)
I tried finding it out on my own, but I have no idea.
Here's my code:
public class RocketMain extends Application{
int width = 640;
int height = 1000;
Pane root;
Stars stars;
public static void main(String[] args){
Application.launch();
}
@Override
public void start(Stage primaryStage) throws Exception {
root = new Pane();
Scene sc = new Scene(root, width, height);
primaryStage.setScene(sc);
primaryStage.setResizable(false);
primaryStage.centerOnScreen();
stars = new Stars(width, height);
new Thread(() -> stars.createStars()).start();
new Thread(() -> update()).start();
root.getChildren().add(stars);
primaryStage.show();
primaryStage.setOnCloseRequest(e -> {
Platform.exit();
System.exit(0);
});
}
public void update(){
while(true){
Platform.runLater(() -> {
stars.getChildren().removeAll(stars.getChildren());
stars.getChildren().addAll(stars.stars);
root.getChildren().removeAll(root.getChildren());
root.getChildren().add(stars);
});
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
The Stars Class which keeps spawning new sprites and moves/deletes them when offscreen:
public class Stars extends Pane {
public ArrayList<Rectangle> stars;
ArrayList<Rectangle> rem;
double width, height;
public Stars(int width, int height){
stars = new ArrayList<>();
rem = new ArrayList<>();
this.width = width;
this.height = height;
}
public void createStars(){
int slowing = 3;
while(true){
if (Math.random() < (1.0/slowing)){
int size = (int) Math.floor(Math.random()*8);
Rectangle temp = new Rectangle(Math.random() * width, -10, size, size);
temp.setFill(Color.BLACK);
this.stars.add(temp);
Platform.runLater(() -> {
this.getChildren().remove(temp);
this.getChildren().add(temp);
});
}
for (Rectangle r : stars) {
r.setY(r.getY() + r.getHeight() / slowing);
if (r.getY() > height / 2) {
rem.add(r);
}
}
stars.removeAll(rem);
rem.clear();
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
tia I hope this was understandable