as the title says, im trying to change an image in a jframe using a timer. but it throws a NullPointerException. I am pretty new at java, and while i am in the process of researching exception handling, any direction you can give me will help my learning greatly. here is my code:
public class PointTest extends JPanel {
JLabel drawcloseLabel = new JLabel(new ImageIcon("src/images/draw_closeup.jpg"));
JLabel monsterAttack = new JLabel("src/images/monsterAttack.jpg");
JLabel endGameLabel = new JLabel(new ImageIcon("src/images/monsterAttack.jpg"));
private static final long serialVersionUID = 1L; // i dont get this
private JFrame frame;
boolean off = false;
public JButton opendraw;
// public JButton button2;
public JButton ok;
public JButton back;
public JButton pickUpKey;
public JButton Key;
public JButton unlockDoor;
public JButton lockedDoor;
public JButton btnNewButton;
JPanel background;
JPanel door;
JPanel draw;
JPanel drawopen;
JPanel endGame;
public void room() {
frame = new JFrame("room");
frame.setSize(1280, 960);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLayeredPane layeredPane = new JLayeredPane();
frame.getContentPane().add(layeredPane, BorderLayout.CENTER);
background = new JPanel();
endGame = new JPanel();
JLabel endGameLabel = new JLabel(new ImageIcon("src/images`/OutOfTime.jpg"));`
JLabel picLabel = new JLabel(new ImageIcon("src/images/room.jpg"));
background.add(picLabel);
endGame.add(endGameLabel);
background.setBounds(0, 0, 1280, 960);
layeredPane.add(background, JLayeredPane.DEFAULT_LAYER);
}
this is the method that, if i catch the exception, will continue the countdown to 0, but will not change the image.
public void EndTimer() {
//error
background.setVisible(false);
endGame.setVisible(true);
endGame.add(endGameLabel);
endGame.setBounds(0, 0, 1280, 960);
}
}
the countdown class for the Timer.
import java.util.Timer;
import java.util.TimerTask;
public class Countdown {
static int interval;
static Timer timer;
boolean off;
PointTest pt = new PointTest();
public void Countdown1() {
int seconds = 10;
int delay = 1000;
int period = 1000;
timer = new Timer();
interval = seconds;
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
System.out.println(setInterval());
if (interval == 7) {
System.out.println("works");
pt.EndTimer();
}
}
}, delay, period);
}
private int setInterval() {
if (interval == 1)
timer.cancel();
return --interval;
}
public int getInterval() {
return this.setInterval();
}
}
import java.io.IOException;
public class windowMain {
/**
* Launch the application.
*
*/
public static void main(String[] args) throws IOException {
Countdown cd = new Countdown();
cd.Countdown1();
PointTest point = new PointTest();
point.room();
}
}
Stacktrace:
Exception in thread "Timer-0" works
test
java.lang.NullPointerException
at PointTest.EndTimer(PointTest.java:207)
at Countdown$1.run(Countdown.java:27)
at java.util.TimerThread.mainLoop(Unknown Source)
at java.util.TimerThread.run(Unknown Source)