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

Java 当我尝试启动计时器时获取NullPointerException,java,timer,Java,Timer,编辑:我有一个打字错误,一发布就看到了。很抱歉浪费大家的时间。谢谢 我正在写一个基本上是太空入侵者的游戏,有两个敌人持续出现并在画面上移动。我试图用定时器来设置它们的运动动画,但当我尝试启动cTimer(中国对象的定时器)时,我遇到了一个例外 以下是中国类的代码: public class GamePanel extends JPanel { Launcher launcher1; Background bground1; public static Shot shot

编辑:我有一个打字错误,一发布就看到了。很抱歉浪费大家的时间。谢谢

我正在写一个基本上是太空入侵者的游戏,有两个敌人持续出现并在画面上移动。我试图用定时器来设置它们的运动动画,但当我尝试启动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;
    }

}
公共类游戏面板扩展JPanel{
发射装置1;
背景1;
公众静态射击;
公共柜台;
公共国际帐户;
公共帐户;
定时器;
定时器定时器;
定时器;
俄罗斯,;
中国,;
public GamePanel()引发IOException{
超级();
这个.shotCounter=0;
launcher1=新的启动器();
bground1=新背景();
计时器=新计时器(10,新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
shot.moveY();
重新油漆();
}
});
rTimer=新计时器(10,新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
俄罗斯。移动();
重新油漆();
}
});
rTimer=new Timer(10,new ActionListener(){//命名错误
@凌驾
已执行的公共无效操作(操作事件e){
中国;
重新油漆();
}
});
addRussianEnemy();
addChineseEnemy();
}//端构造函数
@凌驾
受保护组件(图形g){
超级组件(g);
g、 drawImage(bground1.background,0,0,getWidth(),getHeight(),null);
g、 drawImage(launcher1.baldEagleImage,launcher1.getLxCoord(),launcher1.lyCoord,null);//绘制启动器
如果(rCount==1){
g、 drawImage(rusia.image,rusia.getXCoord(),rusia.getYCoord(),null);
rTimer.start();
}
如果(cCount==1){
g、 drawImage(china.image,china.getXCoord(),china.getYCoord(),null);
cTimer.start();
}
如果(shotCounter==1){
g、 drawImage(shot.mcDShotImage,shot.staticXLauncherCoord,shot.getSyCoord(),null);
timer.start();
}
}//端部元件法
公共无效移动(游戏面板游戏面板){
1.moveX();
重新油漆();
}//端移法
公共void addShot(){
试一试{
快照=新快照();
shotCounter=1;
重新油漆();
}捕获(IOEX异常){
}
}
公共无效addRussianEnemy(){
试一试{
俄罗斯=新俄罗斯();
rCount=1;
重新油漆();
}捕获(IOEX异常){
}
}
公共无效地址Chineseenemy(){
试一试{
中国=新中国();
cCount=1;
重新油漆();
}捕获(IOEX异常){
}
}
}//结束游戏面板类
公共类中国实现实体{
private int xCoord;//对象的x坐标
private int yCoord;//对象的y坐标
private int rise;//对象的y变化
private int run;//对象的x更改
缓冲图像;
公共中国()是一个例外{
this.image=ImageIO.read(新文件(“chinaflag.jpg”);
上升=设置开始上升();
运行=设置开始运行();
xCoord=setStartX();
yCoord=setStartY();
}
@凌驾
公共空间上升(国际上升){
这个上升=上升;
}
@凌驾
公共void setRun(int run){
this.run=run;
}
@凌驾
公共int getRise(){
把这个还给我;
}
@凌驾
public int getRun(){
返回这个.run;
}
@凌驾
公共int setStartRise(){
返回17;
}
@凌驾
public int setStartRun(){
返回17;
}
@凌驾
公开作废动议(){
如果((getXCoord()<(0-getRun())| |(getXCoord()>(800-image.getWidth())){
setRun(-getRun());
}
如果((getYCoord()<(0-getRise())| |(getYCoord()>(500-image.getHeight())){
setRise(-getRise());
}
moveX();
moveY();
}
@凌驾
公共void moveX(){
xCoord+=运行;
}
@凌驾
公共无效动议(){
yCoord+=上升;
}
@凌驾
公共无效setXCoord(int xCoord){
this.xCoord=xCoord;
}
@凌驾
public int getXCoord(){
返回此.xCoord;
}
@凌驾
公共无效设置目录(int yCoord){
this.yCoord=yCoord;
}
@凌驾
public int getYCoord(){
返回此.yCoord;
}
@凌驾
公共int setStartX(){
int xCoord;
xCoord=(int)(Math.random()*51);
返回xCoord;
}
@凌驾
公共int setStartY(){
国际协调;
yCoord=(int)(Math.random()*50);
回归和谐;
}
}

您从未初始化计时器,因此默认为空。

您从未初始化计时器,因此默认为空。

解决方法很简单:您从未初始化计时器


看起来像是剪切粘贴错误,因为您初始化了
rTimer
两次。将第二个更改为
cTimer

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


看起来像是剪切粘贴错误,因为您初始化了
rTimer
两次。将第二个更改为
cTimer

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

cTimer = new Timer();

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

cTimer = new Timer();

您可以双重初始化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
您需要双重初始化
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();
    }
});
你需要初始化