【问题标题】:Getting Exception in "AWT-EventQueue-0" Thread Error在“AWT-EventQueue-0”线程错误中出现异常
【发布时间】:2013-04-06 20:40:10
【问题描述】:

我知道这个问题已经被问了一百万次了,但我看过很多答案,但它们对我没有任何帮助。我不知道我做错了什么。

我相信我收到错误的原因是由于这里的代码:

        RollerBall roller = new RollerBall(game);
        roller.setPosition(new Vec2(-50,-120));

我正在使用上面的代码调用一个类,代码如下:

package game;

import city.soi.platform.*;
import fsm.FSM;

public class RollerBall extends Body implements StepListener {

    public static final float RANGE = 150;

    private Game game;
    private FSM<RollerBall> fsm;

    public RollerBall(Game game) {
        super(game.getWorld());
        game = game;
        fsm = new FSM<RollerBall>(this);
        fsm.start(new StandStillState());
        getWorld().addStepListener(this);
    }


    public boolean inRangeLeft() {
        Player p = game.getPlayer();
        float gap = getPosition().x - p.getPosition().x;
        return gap < RANGE && gap > 0;
    }

    public boolean inRangeRight() {
        Player p = game.getPlayer();
        float gap = getPosition().x - p.getPosition().x;
        return gap > -RANGE && gap < 0;
    }

    public boolean inRange() {
        return inRangeLeft() || inRangeRight();
    }

    public void preStep(StepEvent e) {
        fsm.update();
    }

    public void postStep(StepEvent e) {}
}

最后,当我尝试这样做时,我收到以下错误:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at game.RollerBall.inRangeLeft(RollerBall.java:23)
    at game.StandStillState.update(StandStillState.java:10)
    at fsm.FSM.update(FSM.java:47)
    at game.RollerBall.preStep(RollerBall.java:39)
    at city.soi.platform.World.preStep(World.java:495)
    at city.soi.platform.World.step(World.java:328)
    at city.soi.platform.World$1.actionPerformed(World.java:206)
    at javax.swing.Timer.fireActionPerformed(Timer.java:312)
    at javax.swing.Timer$DoPostEvent.run(Timer.java:244)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:721)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:682)
    at java.awt.EventQueue$3.run(EventQueue.java:680)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:691)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)

提前感谢您的帮助。

【问题讨论】:

  • 调试在提问之前,所有类都出现异常

标签: java swing exception


【解决方案1】:

代码:

    RollerBall roller = new RollerBall(game);
    roller.setPosition(new Vec2(-50,-120));

堆栈跟踪中未提及。

这个在堆栈跟踪中提到:

at game.RollerBall.inRangeLeft(RollerBall.java:23)
at game.StandStillState.update(StandStillState.java:10)
at fsm.FSM.update(FSM.java:47)
at game.RollerBall.preStep(RollerBall.java:39)

所以错误在这里某处,但你没有显示行号:

public boolean inRangeLeft() {
    Player p = game.getPlayer();
    float gap = getPosition().x - p.getPosition().x;
    return gap < RANGE && gap > 0;
}

gamegetPosition()pp.getPosition() 也是 null?

其实,如果行号和发帖一致,我们就可以算出来。如果:

game.RollerBall.preStep(RollerBall.java:39)

然后我们可以倒数到第 23 行,就是这一行:

    Player p = game.getPlayer();

所以我猜game 为空。

编辑 - 查看您的构造函数:

game = game;

这不会将game 分配给您班级的game 字段。你需要这个:

this.game = game;

【讨论】:

  • 感谢保罗的帮助!以及我如何弄清楚为什么它会变为空,也许我初始化它错误?
  • 是的,它符合要求,但是,由于某种原因,实际的滚球并没有出现。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多