Question

edit: I had a typo, saw it as soon as I posted. Sorry to waste everybody's time. Thanks!

I'm writing a game that is basically space invaders, with two enemies persistently present and moving around the frame. I'm trying to animate their movements with a timer, but I'm getting an exception when I try to start the cTimer (timer for the China object).

Here's the code with the China class below:

public class GamePanel extends JPanel {

    Launcher launcher1;
    Background bground1;
    public static Shot shot;
    public int shotCounter;
    public int rCount;
    public int cCount;
    Timer timer;
    Timer rTimer;
    Timer cTimer;
    Russia russia;
    China china;


    public GamePanel() throws IOException {
        super();
        this.shotCounter = 0;
        launcher1 = new Launcher();
        bground1 = new Background();
        timer = new Timer(10, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                shot.moveY();
                repaint();
            }
        });
        rTimer = new Timer(10, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                russia.move();
                repaint();
            }
        });
        rTimer = new Timer(10, new ActionListener() { // misnamed it
            @Override
            public void actionPerformed(ActionEvent e) {
                china.move();
                repaint();
            }
        });
        addRussianEnemy();
        addChineseEnemy();
    }//end constructor

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(bground1.background, 0, 0, getWidth(), getHeight(), null);
        g.drawImage(launcher1.baldEagleImage, launcher1.getLxCoord(), launcher1.lyCoord, null);//paint the launcher
        if (rCount == 1) {
            g.drawImage(russia.image, russia.getXCoord(), russia.getYCoord(), null);
            rTimer.start();
        }
        if (cCount == 1) {
            g.drawImage(china.image, china.getXCoord(), china.getYCoord(), null);
            cTimer.start();
        }
        if (shotCounter == 1) {
            g.drawImage(shot.mcDShotImage, shot.staticXLauncherCoord, shot.getSyCoord(), null);
            timer.start();
        }
    }//end paintComponent method

    public void move(GamePanel gamePanel) {
        launcher1.moveX();
        repaint();
    }//end move method


    public void addShot() {
        try {
            shot = new Shot();
            shotCounter = 1;
            repaint();
        } catch (IOException ex) {
        }
    }

    public void addRussianEnemy() {
        try {
            russia = new Russia();
            rCount = 1;
            repaint();
        } catch (IOException ex) {
        }
    }

    public void addChineseEnemy() {
        try {
            china = new China();
            cCount = 1;
            repaint();
        } catch (IOException ex) {
        }
    }
    }//end GamePanel class


    public class China implements Entity {

    private int xCoord;        //the object's x coordinate
    private int yCoord;        //the object's y coordinate
    private int rise;          //the object's y change
    private int run;           //the object's x change
    BufferedImage image;

     public China() throws IOException {
        this.image = ImageIO.read(new File("chinaflag.jpg"));
        rise = setStartRise();
        run = setStartRun();
        xCoord = setStartX();
        yCoord = setStartY();
    }

    @Override
    public void setRise(int rise) {
        this.rise = rise;
    }

    @Override
    public void setRun(int run) {
        this.run = run;
    }

    @Override
    public int getRise() {
        return this.rise;
    }

    @Override
    public int getRun() {
        return this.run;
    }

    @Override
    public int setStartRise() {
        return 17;
    }

    @Override
    public int setStartRun() {
        return 17;
    }

    @Override
    public void move() {
        if ((getXCoord() < (0 - getRun())) || (getXCoord() > (800 - image.getWidth()))) {
            setRun(-getRun());
        }
        if ((getYCoord() < (0 - getRise())) || (getYCoord() > (500 - image.getHeight()))) {
            setRise(-getRise());
        }
        moveX();
        moveY();
    }

    @Override
    public void moveX() {
        xCoord += run;
    }

    @Override
    public void moveY() {
        yCoord += rise;
    }

    @Override
    public void setXCoord(int xCoord) {
        this.xCoord = xCoord;
    }

    @Override
    public int getXCoord() {
        return this.xCoord;
    }

    @Override
    public void setYCoord(int yCoord) {
        this.yCoord = yCoord;
    }

    @Override
    public int getYCoord() {
        return this.yCoord;
    }

    @Override
    public int setStartX() {
        int xCoord;
        xCoord = (int) (Math.random() * 51);
        return xCoord;
    }

    @Override
    public int setStartY() {
        int yCoord;
        yCoord = (int) (Math.random() * 50);
        return yCoord;
    }

}
Was it helpful?

Solution

You should initialize cTimer before using it:

cTimer = new Timer();

OTHER TIPS

You never initialize the timer, so it is null by default.

Easy to solve: You never initialize it.

Looks like a cut & paste error, because you initialize rTimer twice. Change the second one to cTimer.

You double initialize rTimer:

rTimer = new Timer(10, new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        russia.move();
        repaint();
    }
});
//this should be cTimer!
rTimer = new Timer(10, new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        china.move();
        repaint();
    }
});

You need to initialize cTimer

You need to initialize cTimer in your constructor.

public GamePanel() throws IOException {
    super();
    this.shotCounter = 0;
    launcher1 = new Launcher();
    bground1 = new Background();
    timer = new Timer(10, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            shot.moveY();
            repaint();
        }
    });
    rTimer = new Timer(10, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            russia.move();
            repaint();
        }
    });
    rTimer = new Timer(10, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            china.move();
            repaint();
        }
    });
    addRussianEnemy();
    addChineseEnemy();

    // need to initialize cTimer
    cTimer = new Timer();
}//end constructor
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top