0

当我尝试停止正在运行的线程时,我似乎无法弄清楚为什么会出现空指针异常。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 变量设置为静态,但我得到了一个错误。

4

2 回答 2

7

这就是问题:

LocalFTP ftprun = null;

switch(...) {
    case ...:
        ...
        ftprun = new LocalFTP(...); 
        ...
        break;
    case ...:
        ...
        ftprun.requestStop();
        ...
        break;   
}

这是一个局部变量。它仅在不同的 case 块中设置为非空值,因此在您调用requestStop. 它只会在您的actionPerformed方法的不同调用中发生,使用单独的局部变量(将为空)。

听起来这确实是整个对象状态的一部分——因此您应该将其设为对象中的实例字段,而不是局部变量。

于 2013-05-11T13:37:05.620 回答
4

ftprunactionPerformed()方法的局部变量。所以它在你启动一个线程时被初始化,然后超出范围。

单击停止按钮后,将再次调用 actionPerformed() 方法,并将其ftprun局部变量重新初始化为 null。这个变量应该是一个实例字段,而不是一个局部变量。

于 2013-05-11T13:37:48.700 回答