获取
"Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException"
在所有soms1.actionPerformed
行事件的ActionListener
处:
thread.set_state(1);
thread.set_state(2);
thread.reset;
thread.send;
你能否建议在线程中获取空值的位置?此代码以前运行良好,但在代码中发生少量更改后出现此错误。
代码段:
public class soms1 extends JApplet {
static public SOM_thread1 thread = null;
static JApplet japplet = new JApplet();
static JButton go1 = new JButton("go");
static JButton play = new JButton("play");
static JButton reset = new JButton("Reset");
static JButton send = new JButton("send");
static Container c;
static JPanel p1 = new JPanel();
static JPanel p2 = new JPanel();
static JPanel p6 = new JPanel();
Toolkit tool;
public void setup_applet() {
japplet.setVisible(true);
go1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {thread.set_state(1);}
});
play.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {thread.set_state(2);}
});
reset.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {thread.reset();}
});
send.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {thread.send();}
});
}
public void init() {
tool = Toolkit.getDefaultToolkit();
setup_applet();
setup_radios();
setup_layout();
thread = new SOM_thread1();
thread.init();
thread.start();
if (clientSocket != null) {thread.set_state(2);}
}
}
class SOM_thread1 extends Thread{
public void set_state(int f) {state = f;}
public void send() { //code block}
public void reset() {//code block}
public void run() {//code block}
public void init() {reset();}
}
因为我是java的新手,请建议更改 提前谢谢你。
答案 0 :(得分:0)
在初始化init函数内的线程之前,您正在调用setup_applet。
替换此
public void init() {
tool = Toolkit.getDefaultToolkit();
setup_applet();
setup_radios();
setup_layout();
thread = new SOM_thread1();
thread.init();
thread.start();
if (clientSocket != null) {
thread.set_state(2);
}
}
}
有了这个
public void init() {
tool = Toolkit.getDefaultToolkit();
thread = new SOM_thread1();
setup_applet();
setup_radios();
setup_layout();
thread.init();
thread.start();
if (clientSocket != null) {
thread.set_state(2);
}
}
(注意init函数末尾的删除}。还有一个)
另外将线程方法中的}与另一行分开,因为你用//
对它进行评论