-1

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)
noobAlert
  • 27
  • 3
  • The stack trace of the exception is first you should analyse/post here. – Würgspaß Jan 20 '16 at 12:58
  • were the try catch is, any of those lines, i delete one, then the exception becomes the next line, e.g, i delete background.setVisible(false) and endGame.setVisible(true) becomes the exception – noobAlert Jan 20 '16 at 13:01
  • @noobAlert less code, more stack trace. Stack trace is the error message output in all its beauty after the exception occurred and crashed your program. – Würgspaß Jan 20 '16 at 13:06
  • wrote the stack trace :). – noobAlert Jan 20 '16 at 14:27
  • 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). 3) See [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) & [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/q/218384/418556) – Andrew Thompson Jan 20 '16 at 15:09
  • BTW - `JLabel monsterAttack = new JLabel("src/images/monsterAttack.jpg");` You realized that this label will show (very odd looking) text, right? The `String` passed in the constructor for a `JLabel` will be interpreted as text rather than a path to a file to be used as an image icon. – Andrew Thompson Jan 20 '16 at 15:10

1 Answers1

1

There are some problems with attempting to update a Swing data structure from another thread. You need to use invokeAndWait or invokeLater from your timer to make your changes in the Swing event handling thread.

Romain-p
  • 518
  • 2
  • 9
  • 26