当我尝试启动计时器时获取NullPointerException

时间:2014-04-23 15:37:15

标签: java timer

编辑:我有一个错字,我发布后就看到了。很抱歉浪费每个人的时间。谢谢!

我正在写一个基本上是太空入侵者的游戏,两个敌人一直存在并在框架周围移动。我正试图用计时器动画他们的动作,但是当我尝试启动cTimer(中国对象的计时器)时,我得到一个例外。

以下是中国班级的代码:

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;
    }

}

5 个答案:

答案 0 :(得分:2)

您应该在使用之前初始化cTimer

cTimer = new Timer();

答案 1 :(得分:1)

您永远不会初始化计时器,因此默认情况下它为空。

答案 2 :(得分:1)

易于解决:你永远不会初始化它。

看起来像一个切口&amp;粘贴错误,因为您初始化rTimer两次。将第二个更改为cTimer

答案 3 :(得分:1)

您双重初始化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();
    }
});

您需要初始化cTimer

答案 4 :(得分:1)

您需要在构造函数中初始化cTimer

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