当我尝试停止正在运行的线程时,我似乎无法弄清楚为什么会出现空指针异常。ftprun.requestStop()设置 while 循环的值,以便应用程序停止。
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
JButton btn = (JButton) e.getSource();
Thread ftpthread= null;
LocalFTP ftprun = null;
switch (e.getActionCommand()) {
case "Start Sorter":
if(ftp) {
JOptionPane.showMessageDialog(frame, "Sorter and ftp cannot run at the same time");
} else {
sorter=true;
btn.setText("Stop Sorter");
btn.setBackground(SystemColor.green);
}
break;
case "Start ftp":
if(sorter) {
JOptionPane.showMessageDialog(frame, "Sorter and ftp cannot run at the same time");
} else {
ftp=true;
btn.setText("Stop ftp");
btn.setBackground(SystemColor.green);
Config config = new Config();
try {
File cf= new File(Configfile.configfile);
if (cf.exists()) {
config=ConfigurationTools.openconfig(Configfile.configfile);
}
else {
ConfigurationTools.writeconfig(Configfile.configfile, config);
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
ftprun= new LocalFTP(config,config.getlocalftpinterval());
ftpthread=new Thread (ftprun);
ftpthread.start();
}
break;
case "Start Uploader":
uploader=true;
btn.setText("Stop Uploader");
btn.setBackground(SystemColor.green);
break;
case "Stop Sorter":
sorter=false;
btn.setText("Start Sorter");
btn.setBackground(SystemColor.menu);
break;
case "Stop ftp":
ftp=false;
btn.setText("Start ftp");
btn.setBackground(SystemColor.menu);
ftprun.requestStop();
break;
case "Stop Uploader":
uploader=false;
btn.setText("Start Uploader");
btn.setBackground(SystemColor.menu);
break;
}
}
有什么建议么。我试图将 Thread 和 runnable 变量设置为静态,但我得到了一个错误。