2010-03-28 33 views
2

全部,Java - 如何將table [i] [j]元素聲明爲實例變量?

我想編碼一個Connect4遊戲。爲此,我創建了一個P4Game類和一個代表Connect4板的i X j尺寸的P4Board類。

在P4Game,我有以下幾點:

public class P4Game{ 
    //INSTANCE VARIABLES 
    private int nbLines; 
    private int nbColumns; 
    private P4Board [][] position; 

//CONSTRUCTOR 
    public P4Game(int nbLines, int nbColumns){ 
    this.nbColumns = nbColumns; 
    this.nbLines = nbLines; 
    P4Board [][] position = new P4Board [nbLines][nbColumns]; //Creates the table to receive the instances of the P4Board object.*/ 
    for (int i=0; i<nbLines; i++){ 
     for (int j=0; j<nbColumns; j++){ 
     this.position[i][j] = new P4Board(i,j); //Meant to create each object at (line=i, column=j) 
     } 
    } 
    } 

這導致了嵌套循環,我提this.position[i][j]一個NullPointerException。我在這個類的其他方法中引用這些對象,所以我需要它們作爲實例變量。我想這個例外是因爲我沒有在表的開始處列出表元素position[i][j]作爲實例變量。 (1)我的假設是正確的,如果是這樣的話(2)將聲明這種形式的實例變量的語法是什麼?

謝謝大家的幫助,我認識到的是一個非常基本的問題。希望它也會使其他新手受益。

乾杯,

JDelage

+0

給出整個堆棧跟蹤 – Bozho 2010-03-28 16:51:30

+0

它被稱爲一個字段,而不是一個實例變量。 – SLaks 2010-03-28 16:52:26

+1

我見過很多地方使用的「實例變量」。它出什麼問題了? – 2010-03-28 16:56:55

回答

3

看到添加評論...內聯代碼,您是除了一個小細節,在那裏你要創建一個新的變量position罰款,你實際上意味着使用實例變量。

public class P4Game{ 
    //INSTANCE VARIABLES 
    private int nbLines; 
    private int nbColumns; 
    private P4Board [][] position; 

    //CONSTRUCTOR 
    public P4Jeu(int nbLines, int nbColumns){ 
     this.nbColumns = nbColumns; 
     this.nbLines = nbLines; 
     // You're creating a LOCAL variable called position here if you don't comment what's commented:. 
     /*P4Board [][] */position = new P4Board [nbLines][nbColumns]; //Creates the table to receive the instances of the P4Board object.*/ 
     for (int i=0; i<nbLines; i++){ 
      for (int j=0; j<nbColumns; j++){ 
       this.position[i][j] = new P4Board(i,j); //Meant to create each object at (line=i, column=j) 
      } 
     } 
    } 
} 
+0

這照顧了代碼。我需要更深入地閱讀所有評論,以充分了解問題所在。謝謝大家的回覆! – JDelage 2010-03-28 17:15:29

0

你的假設是不正確的。嘗試在作業中尋找更高的錯誤。

1

您在構造函數中重新定義了P4Board [][] position,然後調用this.position(未初始化)(即null)。

1

仔細看看!您正在隱藏實例變量>P4Board [][] position = new P4Board [nbLines][nbColumns];

2

您的假設不正確。

在構造函數中,您正在創建一個與該字段具有相同名稱的局部變量。 (通過編寫P4Board [][]position = ...)這創建了一個局部變量,並且不影響該字段,該字段保持未初始化。您需要刪除P4Board [][],將其從變量聲明更改爲現有字段的分配。 (就像你寫this.nbLines = ...來分配字段)

0

這對我來說是適合的。我替換爲P4Board,因爲你沒有提供它:

public class P4Game 
{ 
    private int nbLines; 
    private int nbColumns; 
    private int [][] position; 

    public static void main(String[] args) 
    { 
     P4Game game = new P4Game(3, 3); 
     System.out.println(game); 
    } 

    public P4Game(int nbLines, int nbColumns) 
    { 
    this.nbColumns = nbColumns; 
    this.nbLines = nbLines; 
    this.position = new int[this.nbLines][this.nbColumns]; 
    for (int i=0; i < this.nbLines; i++) 
    { 
     for (int j=0; j < this.nbColumns; j++) 
     { 
     this.position[i][j] = i+j; 
     } 
    } 
    } 
    public String toString() 
    { 
     StringBuilder builder = new StringBuilder(1024); 

     builder.append('['); 
     for (int i = 0; i < this.nbLines; ++i) 
     { 
      builder.append('{'); 
      for (int j = 0; j < this.nbColumns; ++j) 
      { 
       builder.append(this.position[i][j]).append(','); 
      } 
      builder.append('}'); 
     } 
     builder.append(']'); 

     return builder.toString(); 

    } 
}