2014-10-02 137 views
1

我对于创建这个问题事先表示歉意,我做了.NET,所以我知道问这个问题的“感觉如何”,但是我搜索并试图在Eclipse中调试我的程序,但仍然无法计算出来如何修复它,因为我是Java GUI的第一次使用和第一次使用Eclipse,所以... ...ActionListener上的java.lang.NullPointerException | Java swing

我有小程序Java swing GUI程序,其中有2个按钮,其中一个隐藏(可见设置为false)点击一个按钮时,它会显示隐藏按钮(设置可见为真):

import java.awt.Container; 
import java.awt.FlowLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.WindowAdapter; 
import java.awt.event.WindowEvent; 

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 

public class TestButton extends JPanel { 

    private JFrame mainFrame; 
    private JButton btnShow ; 
    private JButton btnNew; 

    public TestButton() { 
     mainFrame = new JFrame("Test Button"); 

     JButton btnShow = new JButton("Show New Button"); 
     JButton btnNew = new JButton("This is New Button"); 

     Container c = mainFrame.getContentPane(); 
     c.setLayout(new FlowLayout()); 

     c.add(btnShow); 
     c.add(btnNew); 
     btnNew.setVisible(false); 

     btnShow.setMnemonic('G'); 
     btnNew.setMnemonic('N'); 

     mainFrame.addWindowListener(new WindowAdapter() { 
      public void windowClosing(WindowEvent e) { 
       System.exit(0); 
      } 
     }); 

     ShowButtonHandler ghandler = new ShowButtonHandler(); 
     btnShow.addActionListener(ghandler); 

     mainFrame.setSize(250, 150); 
     mainFrame.setLocationRelativeTo(null); 
     mainFrame.setResizable(false); 
     mainFrame.setVisible(true); 
    } 

    class ShowButtonHandler implements ActionListener { 
     public void actionPerformed(ActionEvent e) { 
      btnNew.setVisible(true); 
     } 
    } 

    public static void main(String args[]) { 
     TestButton app = new TestButton(); 
    } 
} 

但是当我点击该按钮,它显示线程“AWT-EventQueue的 - 0”的Java 异常.lang.Null PointerException

错误出现在此行:btnNew.setVisible(true);

以下是完整的跟踪:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException 
    at TestButton$ShowButtonHandler.actionPerformed(TestButton.java:51) 
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) 
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) 
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) 
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source) 
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) 
    at java.awt.Component.processMouseEvent(Unknown Source) 
    at javax.swing.JComponent.processMouseEvent(Unknown Source) 
    at java.awt.Component.processEvent(Unknown Source) 
    at java.awt.Container.processEvent(Unknown Source) 
    at java.awt.Component.dispatchEventImpl(Unknown Source) 
    at java.awt.Container.dispatchEventImpl(Unknown Source) 
    at java.awt.Component.dispatchEvent(Unknown Source) 
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) 
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) 
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) 
    at java.awt.Container.dispatchEventImpl(Unknown Source) 
    at java.awt.Window.dispatchEventImpl(Unknown Source) 
    at java.awt.Component.dispatchEvent(Unknown Source) 
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source) 
    at java.awt.EventQueue.access$400(Unknown Source) 
    at java.awt.EventQueue$3.run(Unknown Source) 
    at java.awt.EventQueue$3.run(Unknown Source) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) 
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) 
    at java.awt.EventQueue$4.run(Unknown Source) 
    at java.awt.EventQueue$4.run(Unknown Source) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) 
    at java.awt.EventQueue.dispatchEvent(Unknown Source) 
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
    at java.awt.EventDispatchThread.run(Unknown Source) 

所以正常时未初始化的对象,但在宣布时,我没有初始化的btnNew空异常情况发生,不是吗?问题在哪里?另外,我想添加一个名为“再次运行”的按钮,点击它,关闭当前窗口并打开新窗口(基本上我想再次运行程序),是否有可能以这种方式或方式我将其归档?

回答

5

你的局部变量隐藏实例变量

变化

JButton btnNew = new JButton("This is New Button"); 

btnNew = new JButton("This is New Button"); 

编辑

根据作为注释你的问题......但那样会更好k新问题或在https://codereview.stackexchange.com/上发布您的代码。

我的意思是说你的内部类ShowButtonHandler取决于外部类TestButton,因为它使用外部类的字段btnNew

class ShowButtonHandler implements ActionListener { 
    public void actionPerformed(ActionEvent e) { 
     btnNew.setVisible(true); 
    } 
} 

但是这种依赖性是不必要的。 ShowButtonHandler只需要引用一个JButton,它必须在执行操作时可见。

因此,在第一步中,只需将按钮作为构造函数参数传递,就可以将依赖关系分解为外部类。

class ShowButtonHandler implements ActionListener { 

    private JButton btnNew; 

    public ShowButtonHandler(JButton btnNew){ 
     this.btnNew = btnNew; 
    } 

    public void actionPerformed(ActionEvent e) { 
     btnNew.setVisible(true); 
    } 
} 

现在你意识到ShowButtonHandler可以更灵活地允许重用。你可以看看类层次结构,你可以看到setVisible可以用于任何JComponent。所以你可以让课程更一般化。

class ShowComponentHandler implements ActionListener { 

    private JComponent component; 

    public ShowComponentHandler(JComponent component){ 
     this.component = component; 
    } 

    public void actionPerformed(ActionEvent e) { 
     component.setVisible(true); 
    } 
} 

由于ShowButtonHandler现在独立于并具有更一般的API它可以被放置在一个自己的编译单元(Java文件)和被重新使用。

在你TestButton类,你仍然可以使用它

ActionListener showComponentAction = new ShowComponentHandler(btnNew); 
btnShow.addActionListener(showComponentAction); 
+0

完美!非常感谢 – 2014-10-02 17:59:30

+0

让我问你这个快速问题,我想添加一个名为“再次运行”的按钮,点击它,关闭当前窗口并打开新窗口,是否有可能以此方式存档或如何存档? – 2014-10-02 18:04:04

+0

这是可能的。但是我会让处理程序完全独立于TestButton类。只需将'btnNew'传递给构造函数,例如'新的ShowButtonHandler(btnNew);'并将它保存在'ShowButtonHandler'的实例变量中 – 2014-10-02 18:21:22