[英]Getting Exception in “AWT-EventQueue-0” Thread Error
i know this question has been asked like a million times but i have looked on a lot of the answers and they in no way help me. 我知道这个问题已经被问了一百万遍了,但是我已经看了很多答案,它们对我没有任何帮助。 i have no idea what i am doing wrong.
我不知道我在做什么错。
I believe the reason i am getting the error is due to this code here: 我相信我收到错误的原因是由于此代码在这里:
RollerBall roller = new RollerBall(game);
roller.setPosition(new Vec2(-50,-120));
I am using the above code to call a class which code is shown below: 我正在使用上面的代码来调用一个类,其代码如下所示:
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) {}
}
Finally, when trying to do this i am getting the following error: 最后,当尝试执行此操作时,出现以下错误:
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)
Thanks in advance for your help. 在此先感谢您的帮助。
The code: 编码:
RollerBall roller = new RollerBall(game);
roller.setPosition(new Vec2(-50,-120));
is not mentioned in the stack trace. 在堆栈跟踪中未提及。
This is mentioned in the stack trace: 在堆栈跟踪中提到了这一点:
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)
So the error is in here somewhere, but you have not shown line numbers: 所以错误在这里的某个地方,但是您没有显示行号:
public boolean inRangeLeft() {
Player p = game.getPlayer();
float gap = getPosition().x - p.getPosition().x;
return gap < RANGE && gap > 0;
}
So is game
, getPosition()
, p
, or p.getPosition()
null? 那么
game
, getPosition()
, p
或p.getPosition()
null?
Actually, if the line numbers are consistent with the posting we can work it out. 实际上,如果行号与过帐一致,我们可以解决。 If:
如果:
game.RollerBall.preStep(RollerBall.java:39)
Then we can count back to line 23 and it's this line: 然后我们可以回到第23行,它是这一行:
Player p = game.getPlayer();
so I'm guessing that game
is null. 所以我猜该
game
为空。
EDIT - Looking at your constructor: 编辑 -查看您的构造函数:
game = game;
This won't assign game
to your class's game
field. 这不会将
game
分配给您班级的game
领域。 You need this: 你需要这个:
this.game = game;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.