2015-06-18 21 views
0

我有一個非常簡單的程序,讓敵人跟隨玩家,玩家可以自由移動x和y軸。問題是,每次我嘗試執行類似player.getx()player.gety()的操作時,它都會返回空指針異常。找到了這個問題的另一個人,但它並沒有解決我的問題。這是我得到的:Java/libgdx - player.getx()和player.gety()

public class SpaceObject { 

public float x; 
public float y; 
protected float x2; 
protected float y2; 

public float getx() { return x; } 
public float gety() { return y; } 
} 

//Here's my camera attempting to access player.getx() and player.gety(), returning with a null pointer exception: 

    camera.position.set(player.getx(), player.gety(), 0); 



// Here's an example of a bullet-enemy detection(I'm trying to perform enemy1.getx() and enemy1.gety(), also gives me a Null pointer exception): 

//bullet-smallenemy1 collision 
    for(int i = 0; i < bullet1.size(); i++){ 
     Bullet1 b = bullet1.get(i); 
     for(int j = 0; j < smallenemy1.size(); j++){ 
      SmallEnemy1 s = smallenemy1.get(j); 
      if(s.contains(b.getx(), b.gety())){ 
       bullet1.remove(i); 
       i--; 
       smallenemy1.remove(j); 
       j--; 
       break; 
      } 

     } 
    } 

我已粘貼下面的所有東西,以防萬一上述不起作用。我知道有很多混亂。我需要的全部是空指針異常。

http://pastebin.com/3CqkNTgd

+2

這與libgdx無關。只是一個編程錯誤。 「玩家」從未在「theGame」中初始化。它是空的。 –

+0

您可以將所有未實例化或初始化爲工作方法的默認對象。但是,我已經解決了這個問題。當時我很不習慣,我忘記寫這些方法。 – Lexeon

回答

0

NullPointerException異常意味着球員不會被實例化可言,或設置爲null。 看來你永遠不會調用init()方法。