0

I am trying to spread a time-taking loop over many threads, and I get a NullPointerException for some reason. I checked if the thread_array was empty, but it wasn't. Any suggestions at what I am doing wrong? Here is my code:

Inner Thread Class

class RowAndThetaThread implements Runnable{
    private int x_0;
    private int y_0;

    public void run() {
        // do something
        thread_semaphore.release();
    }

    public void input_elements(int x, int y) {
        x_0 = x;
        y_0 = y;
        try {
            thread_semaphore.acquire();
        } catch (Exception e) {}
        this.run();
    }

    public void lol() {
        System.out.println("LOLOLOLOL");
    }
}

Thread Calling

    RowAndThetaThread[] thread_arr = new RowAndThetaThread[LENGTH];

    thread_semaphore = new Semaphore(thread_arr.length, false);
    for (int i=0; i<border_que.arr.length; i++) {
        thread_arr[i].lol(); // NullPointerException happens here
    }
4
  • Your threads may be dying. Try and log a message when a thread dies... Commented Jan 11, 2015 at 15:38
  • possible duplicate of What is a Null Pointer Exception, and how do I fix it? Commented Jan 11, 2015 at 15:40
  • Where are you initializing the objects in the array? Commented Jan 11, 2015 at 15:40
  • @OlegEstekhin I don't see how that question is a duplicate. Commented Jan 11, 2015 at 16:08

2 Answers 2

1

The problem is that you are creating an array for RowAndThetaThread objects, but you aren't populating it with those objects. In Java, arrays of objects are null by default.

  // You declare the array here
RowAndThetaThread[] thread_arr = new RowAndThetaThread[LENGTH];
...

thread_arr[i].lol(); // NullPointerException happens here

Because the array is full of nulls, you get a NullPointerException there. To fix this, you'll have to populate the array. Something like this:

RowAndThetaThread[] thread_arr = new RowAndThetaThread[LENGTH];
for(int i = 0; i < thread_arr.length; i++) {
    thread_arr[i] = new RowAndThetaThread();
}
thread_semaphore = new Semaphore(thread_arr.length, false);
for (int i=0; i<border_que.arr.length; i++) {
   thread_arr[i].lol(); // NullPointerException happens here
}
Sign up to request clarification or add additional context in comments.

Comments

1

You create thread array but do not initialize the array element, so initialize the thread array element. See the following code snippet

RowAndThetaThread[] thread_arr = new RowAndThetaThread[LENGTH];

// Initialization of thread_arr element
for (int i=0; i<border_que.arr.length; i++) {
    thread_arr[i] = new RowAndThetaThread();
}

thread_semaphore = new Semaphore(thread_arr.length, false);
for (int i=0; i<border_que.arr.length; i++) {
    thread_arr[i].lol(); // NullPointerException happens here
}

1 Comment

No, it just create an array not initialize all the element of the array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.