2013-12-18 89 views
0

首先,我實例化一個遊戲狀態獲得一個NullPointerException,不知道爲什麼

class GameState extends state{ 
ArrayList<Level> levels; 
int currentLevelID; 
public GameState() { 
    stateID = 2; 
    levels = new ArrayList<Level>(); 
    createLevels(); 
    currentLevelID = 0; 
} 

它創建使用這段代碼(忘了技術術語笑)

//level is the superclass of normallevel or whatever i named the default level 
    public Level(int id, int height, int width, ArrayList<TileEntity> tiles, ArrayList<MobileEntity> mobiles, TileEntity fillBlock){ 
    this.height = height; 
    this.levelID = id; 
    this.width = width; 
    if(tiles != null){ 
     this.tiles = tiles; 
    } else { 
     tiles = new ArrayList<TileEntity>(); 
     try { 
      tiles.add(new EmptyTile(-50,-50,1,1,null)); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 
    if(mobiles != null){ 
     this.mobiles = mobiles; 
    } else { 
     mobiles = new ArrayList<MobileEntity>(); 
     try { 
      mobiles.add(new DefaultEntity(-50,-50,1,1,null)); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 
    if(fillBlock != null){ 
     this.tiles = new ArrayList<TileEntity>(); 
     this.fillWith(fillBlock); 
    } 
} 
public void fillWith(TileEntity tile){ 
    for(int i = 0; i < this.height; i++){ 
     for(int j = 0; j < this.width; j++){ 
      for(int k = 0; k < this.tiles.size(); k++){ 
       if (this.tiles.get(k).y == i && this.tiles.get(k).x == j){ 
        break; 
       } 
       if(k == this.tiles.size()){ 
        tile.x = j; 
        tile.y = i; 
        this.tiles.add(tile); 
       } 
      } 
     } 
    } 
} 
水平

public void createLevels(){ 
    try { 
     this.levels.add(new NormalLevel(0, 10, 10, null, null, new EmptyTile(1, 1, 1, 1, null))); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

然後我嘗試更新等級

public void update(){ 
    this.draw(); 
    for(int i = 0; i < this.tiles.size(); i++){ 
     this.tiles.get(i).update(); 
    } 
    for(int i = 0; i < this.mobiles.size(); i++){ //nullpointerexception here 
     this.mobiles.get(i).update(); 
    } 

    this.specialUpdate(); 
    } 

但是在註釋行中出現錯誤。我很困惑的地獄,幫助將不勝感激:)

+0

查看代碼,它看起來像手機的ArrayList是空 – Makky

+2

我懷疑你真的想'this.mobiles = new ArrayList ();' – fvu

回答

1
if(mobiles != null){ 
    this.mobiles = mobiles; 
} else { 
    mobiles = new ArrayList<MobileEntity>(); 
    try { 
     mobiles.add(new DefaultEntity(-50,-50,1,1,null)); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 

如果mobiles != null您指定的實例變量(this.mobiles),但如果是這樣,您創建一個新的List,但其分配給本地變量( mobiles),這就是爲什麼列表永遠不會到達實例變量。

+0

我很笨,謝謝你:D – user2155333

2

mobiles對象爲空,因爲在下面的代碼

if(mobiles != null){ 
     this.mobiles = mobiles; 
} else { 
    mobiles = new ArrayList<MobileEntity>(); 

在其他情況下,你分配局部變量mobiles而非this.mobiles

+0

這不是初始化手機的好地方,除了this.mobiles仍然是空的。 – klarki

+0

@klarki我沒有建議在那裏初始化'mobiles'。我剛剛給了NPE的理由 –

相關問題