Java 线程 - NullPointerException
Java Threads - NullPointerException
我试图在多个线程上传播一个耗时循环,但出于某种原因我得到了一个 NullPointerException
。我检查了 thread_array
是否为空,但事实并非如此。对我做错了什么有什么建议吗?这是我的代码:
内螺纹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");
}
}
线程调用
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
}
您创建线程数组但未初始化数组元素,因此初始化线程数组元素。看下面的代码片段
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
}
问题是您正在为 RowAndThetaThread
个对象创建一个数组,但您没有用这些对象填充它。在 Java 中,对象数组默认为 null。
// You declare the array here
RowAndThetaThread[] thread_arr = new RowAndThetaThread[LENGTH];
...
thread_arr[i].lol(); // NullPointerException happens here
因为数组中全是空值,所以你会得到一个 NullPointerException
。要解决此问题,您必须填充数组。像这样:
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
}
我试图在多个线程上传播一个耗时循环,但出于某种原因我得到了一个 NullPointerException
。我检查了 thread_array
是否为空,但事实并非如此。对我做错了什么有什么建议吗?这是我的代码:
内螺纹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");
}
}
线程调用
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
}
您创建线程数组但未初始化数组元素,因此初始化线程数组元素。看下面的代码片段
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
}
问题是您正在为 RowAndThetaThread
个对象创建一个数组,但您没有用这些对象填充它。在 Java 中,对象数组默认为 null。
// You declare the array here
RowAndThetaThread[] thread_arr = new RowAndThetaThread[LENGTH];
...
thread_arr[i].lol(); // NullPointerException happens here
因为数组中全是空值,所以你会得到一个 NullPointerException
。要解决此问题,您必须填充数组。像这样:
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
}