Josh
Josh

Reputation: 221

nullpointerexception with multithreading

my program is to complete Conway's game of life using multithreading. i keep getting the error Exception in thread "main" java.lang.NullPointerException at life.main(life.java:51)

the exception is thrown at different parts of its execution, never actually completing the first threading in its entirety. any help would be appreciated.

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

}

Upvotes: 1

Views: 163

Answers (1)

Reimeus
Reimeus

Reputation: 159874

You are getting an NPE as you never assign a Thread at index position 0:

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

so the exception is thrown when attempting to access it here:

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

Upvotes: 2

Related Questions