-1

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.

user3383620
  • 31
  • 1
  • 6
  • `but the new window wont open and it calls an error.` - well, I'm not a mind reader. I can't guess what the error is. – camickr May 05 '14 at 14:42
  • Ooop sorry, my bad, added now. – user3383620 May 05 '14 at 14:44
  • 1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete and Verifiable Example). 2) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) – Andrew Thompson May 05 '14 at 22:02

1 Answers1

1

Well, a NullPointerException is the most common Exception you will have so you need to learn how to debug the stack trace. The trace tells you the line numbers that caused the exception so you need to find out which variable is null and fix it. We don't have access to your code so you need to do the debugging.

A couple of comments:

  1. You don't need to make a window non-visible AFTER you dispose of the window.

  2. Updates to Swing components should be done on the Event Dispatch Thread, which means you should be using a Swing Timer, not a TimerTask to schedule the event.

camickr
  • 321,443
  • 19
  • 166
  • 288