Java 合线困难

Java 合线困难,java,multithreading,nullpointerexception,Java,Multithreading,Nullpointerexception,当我试图停止运行的线程时,我似乎不明白为什么会出现空指针异常。requestStop()设置while循环的值,以便应用程序停止 public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub JButton btn = (JButton) e.getSource(); Thread ftpthread= null; LocalFTP f

当我试图停止运行的线程时,我似乎不明白为什么会出现空指针异常。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变量设置为static,但我刚得到一个错误。

这就是问题所在:

LocalFTP ftprun = null;

switch(...) {
    case ...:
        ...
        ftprun = new LocalFTP(...); 
        ...
        break;
    case ...:
        ...
        ftprun.requestStop();
        ...
        break;   
}
这是一个局部变量。它只在不同的case块中被设置为非null值,因此在调用
requestStop
的情况下它不可能非null。它只会发生在对
actionPerformed
方法的不同调用中,使用单独的局部变量(将为null)


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

ftprun
actionPerformed()
方法的局部变量。因此,当您启动一个线程时,它被初始化,然后超出范围


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

如果您能说出从何处获得异常,这会有所帮助。如果您能给出一个简短但完整的示例来说明问题,这也会很有帮助。正如我所说,当尝试停止使用ftprun.requestStop()完成的线程时,我会遇到空指针异常。我将在问题中提到这一点。堆栈跟踪告诉我的是,当我尝试停止线程时,ftprun为null,我不明白为什么。所以你是说我需要将变量移出actionPerformed()并作为对象的一部分启动它们。谢谢,这很有效。@Codeguy007:好的。你明白为什么它会起作用吗?理解“当前正在执行的操作”在逻辑上是对象状态的一部分很重要,并且状态需要存储在字段中,而不是实例变量中。