I am trying to open a JFrame and dispose of my current one on the occurrence of an if statement within a timer, I have tried both directly inserting the window in the if statements setting it to visible and tried making a method which is called in the if statement which should open and close the frames, neither of which has worked. Any help would be much appreciated, here's the relevant code snippets:
At the top of the class:
private DieWindow dieWindow;
Further down:
public void setAgeTimer() {
int period = 10;
int delay = 10;//7000
ageBar.repaint();
ageTimer = new Timer();
ageTimer.scheduleAtFixedRate(new TimerTask() {
public void run() {
setAgeInterval();
}
}, delay, period);
}
//Interval method for age bar
public void setAgeInterval() {
if(snowLeopard.getAge() < snowLeopard.getLifespan()) {
ageInt++;
ageBar.setValue(ageInt);
if (ageInt == 100) {
snowLeopard.incAge();
ageLabel.setText("I am " + snowLeopard.getAge() + " years old with");
ageInt = 0;
}
}
else if(snowLeopard.getAge() == snowLeopard.getLifespan()) {
ageTimer.cancel();
terminate();
}
}
public void terminate() {
snowLeopardWindow.dispose();
snowLeopardWindow.setVisible(false);
dieWindow.getMainDieWindow().setVisible(true);
}
and in the dieWindow class there is:
public JFrame getMainDieWindow() {
return mainDieWindow;
}
public DieWindow(int width, int height, SnowLeopard snowLeopard) {
JFrame mainDieWindow = new JFrame();
//mainDieWindow.setTitle("Your pet has died :(");
mainDieWindow.setSize(width, height);
mainDieWindow.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.snowLeopard = snowLeopard;
Plus all the creation details such as height, color etc, I have added a main method to this class and ran it and the window does pop up (main method now commented out), so I know that the JFrame itself will work and appear.
The main program runs fine, then when when it reaches its allotted time where age = 20, it closes down, but the new window wont open and it calls an error.
Exception in thread "Timer-12" java.lang.NullPointerException
at SnowLeopardInterface.terminate(SnowLeopardInterface.java:562)
at SnowLeopardInterface.setAgeInterval(SnowLeopardInterface.java:554)
at SnowLeopardInterface$8.run(SnowLeopardInterface.java:535)
at java.util.TimerThread.mainLoop(Unknown Source)
at java.util.TimerThread.run(Unknown Source)
Many thanks if you can help.