2011-04-08 119 views
0

我正在開發一個Applet,它有一個JButton,我想用它來啓用另一個JButton。但是,當我按下按鈕時,出現錯誤:線程「AWT-EventQueue-1」java.lang.NullPointerException中的異常。這是爲什麼發生?看起來好像當我運行Applet時,全局變量不會被實例化(即它們都是「null」)。在另外一個程序中,一切正常,在執行這個操作方面我找不到兩者之間的區別。Java Applet空指針異常

這裏有點我的代碼:

public class implementation2 extends Applet implements ActionListener { 
    private static final long serialVersionUID = -4370650602318597069L; 
    ... 
    public JButton coreButton, testButton; 
    ... 
    public void init() { 
    ... 
    final JButton testButton = new JButton("Test); 
    testButton.addActionListener(this); 
    ... 
    final JButton coreButton = new JButton("CORE"); 
    coreButton.addActionListener(this); 
    coreButton.setEnabled(false); 
    ... 
    } 
    ... 
    public void actionPerformed(final ActionEvent event) { 

    if(event.getActionCommand() == "Test") { 
    coreButton.setEnabled(false); 
    } 
    ... 

如果任何人都可以向固定我的代碼點我的方向,將不勝感激!謝謝!

+1

你應該有一個堆棧跟蹤 - 看看,以查明發生了什麼事情。你也應該開始使用普通的Java命名約定(類應該在PascalCase中),並且用'equals()'而不是'=='比較字符串。 – 2011-04-08 11:14:49

回答

1

這就是問題所在:

public JButton coreButton, testButton; 

public void init() { 
    final JButton testButton = new JButton("Test); 

在這裏,你已經創建了一個本地變量,它對於陰影的testButton例如變量(與同爲coreButton)。這意味着實例變量仍然爲空 - 因此,當您稍後嘗試解除引用時,您會遇到異常。您不想在init中聲明新的局部變量 - 您只是想將值分配給實例變量。更正後的代碼:

public class Implementation2 extends Applet implements ActionListener { 
    private static final long serialVersionUID = -4370650602318597069L; 
    ... 
    public JButton coreButton, testButton; 
    ... 
    public void init() { 
    ... 
    testButton = new JButton("Test"); 
    testButton.addActionListener(this); 
    ... 
    coreButton = new JButton("CORE"); 
    coreButton.addActionListener(this); 
    coreButton.setEnabled(false); 
    ... 
    } 
    ... 
    public void actionPerformed(final ActionEvent event) { 
    if("Test".equals(event.getActionCommand())) { 
     coreButton.setEnabled(false); 
    } 
    ... 
    } 
} 
+0

啊我明白了。我認爲它看起來很粗略,但我主要重複使用代碼(當然,允許),所以它可以很容易地搞亂。非常感謝你! – TNC 2011-04-08 17:04:30

1

當你宣佈這些全球那麼爲什麼又在初始化宣佈他們init()()只寫:

public void init() { 
    ... 
    testButton = new JButton("Test"); 
    testButton.addActionListener(this); 
    ... 
    coreButton = new JButton("CORE"); 
    coreButton.addActionListener(this); 
    coreButton.setEnabled(false); 
    ... 
    } 

可能的錯誤在你的代碼:

public class implementation2 extends Applet implements ActionListener { 
    private static final long serialVersionUID = -4370650602318597069L; 
    ... 
    public JButton coreButton, testButton; 
    ... 
    public void init() { 
    ... 
    final JButton testButton = new JButton("Test); //---- Duplicate declaration which should not be done. 
    //---- Forgot to write `"` to finish `Test` string 
    testButton.addActionListener(this); 
    ... 
    final JButton coreButton = new JButton("CORE"); //---- Duplicate declaration which should not be done. 
    coreButton.addActionListener(this); 
    coreButton.setEnabled(false); 
    ... 
    } 
    ... 
    public void actionPerformed(final ActionEvent event) { 

    if(event.getActionCommand() == "Test") { //--- use `.equals()` instead of `==` 
    coreButton.setEnabled(false); //---- set it to `true` instead of `false`. 
    } 
0

在init()中,你創建了兩個隱藏外部按鈕的局部按鈕,因此它們在init()之後仍然爲空。