2013-04-06 41 views
0

我知道這個問題已被問過像百萬次,但我看了很多答案,他們絕不會幫助我。我不知道我做錯了什麼。在「AWT-EventQueue-0」中獲得例外線程錯誤

我相信,我收到錯誤的原因是由於該代碼在這裏:

 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) 

在此先感謝您的h ELP。

+0

調試之前問一個問題,異常來自所有類 – mKorbel 2013-04-06 20:46:10

回答

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; 
+0

謝謝保羅試圖幫助!我怎麼會找出爲什麼它會出現空,也許我初始化它錯了? – 2013-04-06 20:52:52

+0

看到編輯... – 2013-04-06 20:53:20

+0

是的,它符合,但是,實際上滾球並不出於某種原因。 – 2013-04-06 20:54:13