2014-10-02 133 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