Java线程-NullPointerException
我试图在许多线程上传播一个耗时的循环,由于某种原因,我得到了一个Java线程-NullPointerException,java,android,multithreading,nullpointerexception,Java,Android,Multithreading,Nullpointerexception,我试图在许多线程上传播一个耗时的循环,由于某种原因,我得到了一个NullPointerException。我检查了线程数组是否为空,但它不是空的。对我做错了什么有什么建议吗?这是我的密码: 内螺纹等级 class RowAndThetaThread implements Runnable{ private int x_0; private int y_0; public void run() { // do something threa
NullPointerException
。我检查了线程数组是否为空,但它不是空的。对我做错了什么有什么建议吗?这是我的密码:
内螺纹等级
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
}
rownthetathread[]thread\u arr=新的rownthetathread[LENGTH];
thread_信号量=新信号量(thread_arr.length,false);
对于(int i=0;i您创建了线程数组,但没有初始化数组元素,因此初始化线程数组元素
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
}
rownthetathread[]thread\u arr=新的rownthetathread[LENGTH];
//线程\u arr元素的初始化
对于(int i=0;i而言,问题在于您正在为行和线程
对象创建一个数组,但没有用这些对象填充它。在Java中,对象数组默认为空
// 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
}
rownthetathread[]thread\u arr=新的rownthetathread[LENGTH];
对于(int i=0;i 对于(int i=0;i)您的线程可能正在死亡。当线程死亡时,请尝试记录一条消息…您在哪里初始化数组中的对象可能存在重复?@OlegEstekhin我看不出该问题是重复的。不,它只是创建一个数组,而不是初始化数组中的所有元素