我有2個類(1是基本類,第二個擴展了Thread類),我試圖訪問一個對象(類),該對象在我的線程類中初始化setText()
從另一個線程訪問一個對象
public class TThread extends Thread{
Patcher pf;
public TThread(String string) {
setName(string);
start();
}
@Override
public void run() {
pf = new Patcher("Checking Serial key..."); //<=== Class initialized here in a separate thread
}
public void setText(String string) {
pf.setText(string); //<=== Trying to access patcher here, throws NullPointerException
}
}
這是我怎麼稱呼TThread
public void myCall(){
TThread tpf = new TThread("pf thread");
//some code later
try{
tpf.setText("blabla");
}
的pf.setText()
拋出NullPointerException
當我試圖從另一個線程訪問補丁。
如何才能到達該線程並從另一個班級或此班級訪問修補程序?
在'run'方法中只有構造函數是非常不尋常的,你確定在調用'yourTThreadObject.start()'之後調用'setText()'*嗎? – kajacx
@kajacx我確定我沒有在'start()'之前調用'setText()'。我在一個類中初始化我的線程類,然後在10行後調用'setText()'。 –