1

I am new to using threads, and I'm trying to figure out a way to tell if a thread is terminated, as well as gather some information from the thread. However, I'm getting a null pointer exception whenever I try to call a method of one of the threads including thread.getState(). Please I would like some insight as to how a thread works in java with respect to how I am using it.

public class MatrixThread extends Thread{

private int num;
private String ref;
private boolean finished;
JsonObject json = new JsonObject();

public MatrixThread(int number){
    super("Matrix Thread");
    System.out.println("Running Thread: " +number);
    num = number;
    json = object;
    finished = false;
    start();
}

public void run(){
    System.out.println("Thread #" + num + "Has begun running");
    boolean again = true;

    while(again){
            //Does something
            if(wasSuccessful()) {
                ref = operation
                System.out.println("Success");
                finished = true;
            } else System.out.println("Did not work try again");
        } catch (IOException e) {
            System.out.println("Error, Try again");
        }
    }
}

public boolean isFinished(){
    return finished;
}

public String getRef(){
    return ref;
}

public int getNum(){
    return num;
}
}

And then when I run my program it looks like this

public static void main(String[] args) {
    MatrixThread[] threads = new MatrixThread[10];

    String[] refs = new String[100];
    int count = 0;
    for(MatrixThread thread : threads){
        thread = new MatrixThread(count);
        count++;
    }

    while(count < 100){
        for(MatrixThread thread : threads){
            if(thread.getState() == Thread.State.TERMINATED){
                refs[thread.getNum()] = thread.getRef();
                thread = new MatrixThread(count);
                count++;
            }
        }
    }

}

execution in the main process stops on "thread.getState()" because of a null pointer exception. Any ideas why?

Burkhard
  • 14,596
  • 22
  • 87
  • 108
  • You do not need the tags in the title/questions. I removed it for you, but please edit it to make it more meaningful. – Burkhard Jul 09 '13 at 19:48
  • 1
    If you are new to threads this read this: http://docs.oracle.com/javase/tutorial/essential/concurrency/ – dougEfresh Jul 09 '13 at 19:53
  • 1
    I'm wondering about your main method flow, especially the while loop, seems you need more read about threads, check this, may help http://www.codeproject.com/Articles/616109/Java-Thread-Tutorial –  Jul 09 '13 at 20:50

2 Answers2

2

You are not assigning indices in your thread array to non-null values. You create them, but never assign them to indices in the array, so those indices are null.

Here's a correction to your code:

for(int i=0;i<threads.length;i++){
    MatrixThread thread = new MatrixThread(count);
    threads[i] = thread;
    count++;
}

I don't suggest extending thread. Try implementing runnable, and passing your runnable to a thread instead. I could go into detail why but its already been done.

Thread.isAlive is probably what you're looking for. I suggest doing something like...

runnable.setActive(false);
//this will block invoking thread for 1 second, or until the threadRunningRunnable terminates
threadRunningRunnable.join(1000);
//for the paranoid programmer...
if(threadRunningRunnable.isAlive()){
    //something very bad happened.
}
Community
  • 1
  • 1
William Morrison
  • 10,953
  • 2
  • 31
  • 48
0

thread.getState() is looking for the state of whatever is stored in the index of your array, but since there haven't been any values assigned, they have no state. Thus when getState looks at the array, it isn't finding any state to return.

superdiazepam
  • 457
  • 5
  • 13