爲什麼會出現在這裏的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