2012-02-13 10 views
0

を抱えていることは私のコードです:http://pastebin.com/y5wU0Zpx(LCI)は、事前に構築されたインターフェース(ライフゲーム)で実行するJavaプログラムを書くが、私はここでトラブル

http://pastebin.com/umy0FPvB(LG)

、ここでは、教師のコードです

LCIがLG [world()]から渡された行列を読み込もうとしているときに、先生のコードの41行目が間違っていると言います。

私はしばらくこの座っていましたが、何が間違っているのか分かりません。

Exception in thread "main" java.lang.NullPointerException 
    at Console.printWorld(Console.java:41) 
    at Console.playLife(Console.java:56) 
    at Console.main(Console.java:30) 

-

/** 
* The Life game 
* @author Noah Kissinger 
* @date 2012.2.13 
*/ 

import java.util.Random; 

public class Life { 

    private static boolean[][] matrix; 
    private static int bL, bH, lL, lH, r, c; 
    private static long rSeed; 

    public Life(long seed, int rows, int columns, int birthLow, int birthHigh, 
      int liveLow, int liveHigh) { 

     rSeed = seed; 
     bL = birthLow; 
     bH = birthHigh; 
     lL = liveLow; 
     lH = liveHigh; 
     r = rows; 
     c = columns; 

     createMatrix(); 

    } 

    public void update() { 
     updateMatrix(); 
    } 

    public boolean[][] world() { 
     return matrix; 
    } 

    public static void createMatrix() { 

     Random seedBool = new Random(rSeed); 

     boolean[][] matrix = new boolean[r][c]; 

     for (int i = 0; i < matrix.length; i++) { 
      for (int j = 0; j < matrix[i].length; j++) { 
       matrix[i][j] = false; 
      } 
     } 

     for (int i = 1; i < matrix.length - 1; i++) { 
      for (int j = 1; j < matrix[i].length - 1; j++) { 
       matrix[i][j] = seedBool.nextBoolean(); 
      } 
     } 

    } 

    public static void updateMatrix() { 

     Random seedBool = new Random(rSeed); 

     boolean[][] matrixCopy = matrix.clone(); 
     for (int i = 0; i < matrix.length; i++) 
      matrixCopy[i] = matrix[i].clone(); 

     int count = 0; 

     for (int i = 1; i < matrix.length - 1; i++) { 
      for (int j = 1; j < matrix[i].length - 1; j++) { 

       if (matrix[i][j] == false) { 

        if (matrixCopy[i - 1][j - 1] == true) 
         count++; 
        if (matrixCopy[i - 1][j] == true) 
         count++; 
        if (matrixCopy[i - 1][j + 1] == true) 
         count++; 
        if (matrixCopy[i][j - 1] == true) 
         count++; 
        if (matrixCopy[i][j + 1] == true) 
         count++; 
        if (matrixCopy[i + 1][j - 1] == true) 
         count++; 
        if (matrixCopy[i + 1][j] == true) 
         count++; 
        if (matrixCopy[i + 1][j + 1] == true) 
         count++; 

        if (count >= bL && count <= bH) { 
         matrix[i][j] = true; 

         for (int i1 = 0; i1 < matrix.length; i1++) { 
          for (int j1 = 0; j1 < matrix[i1].length; j1++) { 
           matrix[i1][j1] = false; 
          } 
         } 

         for (int i1 = 1; i1 < matrix.length - 1; i1++) { 
          for (int j1 = 1; j1 < matrix[i1].length - 1; j1++) { 
           matrix[i1][j1] = seedBool.nextBoolean(); 
          } 
         } 
        } else 
         matrix[i][j] = false; 
        count = 0; 

       } 

       else { 

        if (matrixCopy[i - 1][j - 1] == true) 
         count++; 
        if (matrixCopy[i - 1][j] == true) 
         count++; 
        if (matrixCopy[i - 1][j + 1] == true) 
         count++; 
        if (matrixCopy[i][j - 1] == true) 
         count++; 
        if (matrixCopy[i][j + 1] == true) 
         count++; 
        if (matrixCopy[i + 1][j - 1] == true) 
         count++; 
        if (matrixCopy[i + 1][j] == true) 
         count++; 
        if (matrixCopy[i + 1][j + 1] == true) 
         count++; 

        if (count >= lL && count <= lH) 
         matrix[i][j] = true; 
        else 
         matrix[i][j] = false; 
        count = 0; 

       } 
      } 
     } 
    } 
} 

-

/** 
* The Console class is a console interface to the Life game. 
* @author DH 
* @date Sept. 2008 
*/ 

import java.util.Scanner; 


public class Console { 

    /** 
    * @param args unused 
    */ 
    public static void main(String[] args) { 
     Scanner in = new Scanner(System.in); 
     System.out.println("Please enter the size of the matrix(rows, columns) :"); 
     int rows = in.nextInt(); 
     int columns = in.nextInt(); 
     System.out.println("Please enter random seed: "); 
     long seed = in.nextLong(); 
     System.out.println("Please enter birth range (low, high) :"); 
     int birthLow = in.nextInt(); 
     int birthHigh = in.nextInt(); 
     System.out.println("Please enter live range (low, high): "); 
     int liveLow = in.nextInt(); 
     int liveHigh = in.nextInt(); 
     try { 
      Life game = new Life(seed, rows, columns, birthLow, birthHigh, liveLow, liveHigh); 
      playLife(game); 
     } catch (IllegalArgumentException e) { 
      System.out.println("Inappropriate values: " + e.getMessage()); 
     } 
    } 

    /** 
    * Print a boolean matrix 
    * @param world is a boolean matrix to be printed with # for true and - for false. 
    */ 
    public static void printWorld(boolean[][] matrix) { 
     for (int r=0; r<matrix.length; r++) { 
      for (int c=0; c<matrix[0].length; c++) { 
       System.out.print(matrix[r][c] ? " # " : " - "); 
      } 
      System.out.println(); 
     } 
     System.out.println(); 

    } 

    /** 
    * Play the game of Life starting with a given state 
    * @param game is the Life object that provides the current state of Life 
    */ 
    public static void playLife(Life game) { 
     printWorld(game.world()); 
     for (int i=0; i<10; i++) { 
      game.update(); 
      printWorld(game.world()); 
     } 
    } 

} 
+2

正確なエラーは何ですか? – Peter

+0

リンクする代わりに該当するスニペットをここに投稿できますか? – simchona

+0

私はエラーメッセージを追加しましたが、それは約200行のコードなので、ここに投稿しても大丈夫ですか? – Noah

答えて

3

はここにあなたの問題です。フィールドmatrixを実際に変更する場合は、createMatrix()メソッドでローカル変数matrixを定義します。

フィールドには、thisでアクセスすると便利です。 this.matrix。コード内で明確な区別ができます。しかし、ほとんどのIDEはフィールドとローカル変数を自動的にハイライト表示するので、不必要であると感じる人もいます。それはスタイルの問題であり、あまり重要ではありません。

私はあなたのプログラムの残りの部分をチェックしていない、他のエラーがある可能性があります。

public static void createMatrix() { 

    Random seedBool = new Random(rSeed); 

    this.matrix = new boolean[r][c]; 

    for (int i = 0; i < this.matrix.length; i++) { 
     for (int j = 0; j < this.matrix[i].length; j++) { 
      this.matrix[i][j] = false; 
     } 
    } 

    for (int i = 1; i < this.matrix.length - 1; i++) { 
     for (int j = 1; j < this.matrix[i].length - 1; j++) { 
      this.matrix[i][j] = seedBool.nextBoolean(); 
     } 
    } 

} 
+0

うわー、それはどのように固定されているのか面白いです。すでに作成したものを修正するのではなく、新しいものを作成しているとは思っていませんでした。私が今必要とするのは、私の論理を修正することだけです。私はすべて設定する必要があります。 – Noah

1

boolean[][] matrix = new boolean[r][c];

この行はcreateMatrix方法でLOCA変数でそれをブール値の2次元配列を作成して記憶します。 したがって、Lifeクラスの静的フィールドmatrixはまだnullになります。 このフィールドは、worldメソッドを介して読み取られ、playLifeメソッドに渡されます。 そして、printLifeメソッド呼び出しはNPEをトリガーします。

ところで、なぜあなたは多くの静的フィールドやメソッドを使って人生ゲームを実装しましたか?

+0

私は静的と静的ではないことの違いを知りませんでしたので、すべて静的にしました。ええ、私は新しいものを作っていて、グローバルマトリックスを変更していないことに気がつきました。ありがとうございました:) – Noah

関連する問題