1

我的程序是使用多线程来完成康威的人生游戏。我不断收到错误Exception in thread "main" java.lang.NullPointerException at life.main(life.java:51)

异常在其执行的不同部分被抛出,从未真正完整地完成第一个线程。任何帮助,将不胜感激。

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;
}

}

4

1 回答 1

2

你得到一个,因为你从来没有在索引位置NPE分配一个:Thread0

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

因此尝试在此处访问它时会引发异常:

for (Thread t : threads) {
  ...
  t.join(); // NPE
于 2012-12-08T00:00:39.130 回答