2012-12-07 67 views
1

我的計劃是完成Conway的使用多線程遊戲人生。我不斷收到錯誤Exception in thread "main" java.lang.NullPointerException at life.main(life.java:51)NullPointerException異常與多線程

異常是在其執行的不同部分,從來沒有真正完成了全部第一螺紋拋出。任何幫助,將不勝感激。

import java.util.Arrays; 
import java.util.Random; 
import java.util.Scanner; 



public class life { 

final static int rowBound = 9; 
final static int colBound = 9; 
static int numThreads; 
static boolean matrix[][]; 
static boolean prevMatrix[][]; 
static boolean newMatrix[][]; 
static boolean stable; 


public static void main(String[] args) { 

    matrix = new boolean[rowBound+1][colBound+1]; 
    prevMatrix = new boolean[rowBound+1][colBound+1]; 
    newMatrix = new boolean[rowBound+1][colBound+1]; 
    stable = false; 

    Scanner in = new Scanner (System.in); 
    numThreads = 8; 

    matrix = fillMatrix(matrix); 
    Random r = new Random(); 
    matrix = initializeMatrix(matrix, r); 
    printMatrix(matrix); 

    Thread[] threads = new Thread[numThreads]; 

    while (!stable){ 

     for (int i = 1; i < numThreads; ++i) { 
      Runnable prod = new update(i, newMatrix); 
      threads[i] = new Thread(prod); 
      threads[i].start(); 
     } 
     for (Thread t : threads) { 
      try { 
       t.join(); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 
     } 

     stable = compare(prevMatrix, newMatrix); 
     printMatrix(newMatrix); 

     if (!stable){ 
      prevMatrix = give(prevMatrix, matrix); 
      matrix = give(matrix, newMatrix); 
     } 
    } 

} 

// fill matrix with false 
public static boolean[][] fillMatrix(boolean[][] mat) { 
    for (int x = 0; x < rowBound; x++) { 
     for (int y = 0; y < colBound; y++) { 
      mat[x][y] = false; 
     } 
    } 
    return mat; 
} 

// initializes the matrix 
public static boolean[][] initializeMatrix(boolean[][] mat, Random r) { 
    for (int x = 1; x < rowBound; x++) { 
    for (int y = 1; y < colBound; y++) 
      mat[x][y] = r.nextBoolean(); 
    } 
    return mat; 
} 

// print matrix 
public static void printMatrix(boolean[][] matrix) { 
    System.out.println("Dead cell --> '-' Live cell --> '+'"); 
    for (int x = 0; x <= rowBound; x++) { 
     for (int y = 0; y <= colBound; y++) { 
      if (!matrix[x][y]) 
       System.out.print(" -"); 
      else 
       System.out.print(" #"); 
     } 
     System.out.println(); 
    } 
} 

public static boolean compare(boolean[][] prevM, boolean[][] newM) { 
    for (int a = 1; a < rowBound-1; a++){ 
     for (int b = 1; b < colBound-1; b++){ 
      if (prevM[a][b] != newM[a][b]) 
       return false; 
     } 
    } 
    return true; 
} 


static class update implements Runnable { 
    int counter; 
    int numRows; 
    int firstRow; 
    int lastRow; 
    int thread; 


    public update(int i, boolean[][] newMatrix) { 
     thread = i; 
    } 

    @Override 
    public void run() { 

     numRows = 1; 
     firstRow = thread * numRows; 
     lastRow = numRows + firstRow; 
     counter = 0; 
     for (int a = firstRow; a < lastRow; a++) { 
      for (int b = 1; b < colBound; b++) { 
       for (int c = a - 1; c <= a + 1; c++) { 
        for (int d = b - 1; d <= b + 1; d++) { 
         if (matrix[c][d] == true) 
          counter++; 
        } 
       } 

       if (matrix[a][b]) { 
        counter--; 
        if (counter < 4) { 
         if (counter > 1) 
          newMatrix[a][b] = true; 
         else 
          newMatrix[a][b] = false; 
        } else 
         newMatrix[a][b] = false; 
       } else { 
        if (counter == 3) 
         newMatrix[a][b] = true; 
        else 
         newMatrix[a][b] = false; 
       } 


      } 
     } 


    } 
} 

// gives the values from the newMatrix and puts them into matrix 
public static boolean[][] give(boolean[][] matrix, boolean[][] newMatrix) { 
    for (int x = 0; x < rowBound; x++) { 
     for (int y = 0; y < colBound; y++) { 
      matrix[x][y] = newMatrix[x][y]; 
     } 
    } 
    return matrix; 
} 

}

回答

2

您購買的NPE因爲你從來沒有在索引位置0分配Thread

for (int i = 1; i < numThreads; ++i) { 

因此,嘗試在這裏訪問它時,拋出異常:

for (Thread t : threads) { 
    ... 
    t.join(); // NPE 
+0

讓它工作乾淨。謝謝! – Josh