为什么会出现在这里的NullPointerExceptionNullPointerException异常在线程
package Threads;
public class Test implements Runnable {
FF d;
public static void main(String[] args) {
new Test().go();
}
void go() {
System.out.println(Thread.currentThread());
d = new FF();
new Thread(new Test()).start();
new Thread(new Test()).start();
}
public void run() {
System.out.println(d);
System.out.println(Thread.currentThread());
d.chat(Thread.currentThread().getId());//RTE
}
}
class FF {
static long flag = 0;
void chat(long id) {
if(flag == 0) {
flag = id;
}
for(int x=1;x<3;x++) {
if(flag == id) {
System.out.println("Yo");
}else {
System.out.println("dude");
}
}
}
}
//任何人可以解释为什么我收到的NullPointerException虽然这儿过得参考d
变量在go()
方法intialized但我认为它有话在创建新线程时执行。
什么堆栈跟踪说?你应该阅读它来了解问题所在。如果你无法理解,请发布。 – 2014-10-27 17:39:46
你的想法是正确的。谁初始化'd'? 'go()'方法?这是在创建新线程的两行中调用的吗? – 2014-10-27 17:42:15