My program is a game, and it requires sprites to be spawned every 2 seconds. And following is my code so far for the same:
@Override
public void run() {
try {
spawnSprite(300, 300, true);
} catch(NullPointerException e){
e.printStackTrace();
System.exit(0);
}
}
private void spawnSprite(float x, float y, Boolean isPea){
String url;
if(isPea)
url = "/Assets/Pea.png";
else
url = "/Assets/Not_pea.png";
Image image = new Image(getClass().getResourceAsStream(url));
ImageView Im = new ImageView();
Im.setImage(image);
Im.setX(x);
Im.setY(y);
AP.getChildren().add(Im);
}
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
spawnSprite( 300, 300, true);
Timer SpawnTimer = new Timer();
TimerTask task= new Map_Controller();
SpawnTimer.scheduleAtFixedRate(task, 1000L,2000L);
}
}
However AP,getChildren.add(Im)
in spawnSprite()
throws a NullPointerException. I did find out that no exception is thrown if I call the function from initialize()
.
Any suggestions would be very helpful, Thanks!