【发布时间】:2014-07-13 22:50:50
【问题描述】:
我有 2 个类(1 个是基本类,第二个是扩展 Thread 类),我正在尝试使用 setText() 访问在 run() 上的线程类中初始化的对象(类)
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方法中只有构造函数是相当不寻常的,你确定要调用setText()在调用yourTThreadObject.start()吗? -
@kajacx 我确定我不会在
start()之前调用setText()。我在一个类中初始化我的线程类,然后在 10 行之后我调用setText()。
标签: java multithreading