0

東、西、北、南に4つのボタンがあり、押されたボタンでテキストを変更しようとしています。Eclipse では、コンパイルする前は問題ないように見えますが、コンパイルして 4 つのボタンのいずれかを押そうとすると、多くの例外が表示されます。

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

public class gui {
    JButton rightbutton;
    JButton leftbutton;
    JButton downbutton;
    JButton upbutton;

    public static void main(String[] args) {
        gui GUI = new gui();
        GUI.go();
    }

    public void go() {
        JFrame myFrame = new JFrame("The Gui");
        JPanel myPanel = new JPanel();

        JButton rightbutton = new JButton("Right Button");
        rightbutton.addActionListener(new rightListener());
        JButton leftbutton = new JButton ("Button of the west");
        leftbutton.addActionListener(new leftListener());
        JButton downbutton = new JButton ("Button on the bottom");
        downbutton.addActionListener(new downListener());
        JButton upbutton = new JButton ("Button in the sky");
        upbutton.addActionListener(new upListener());   

        myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        myFrame.setSize(500,500);
        myFrame.setVisible(true);

        myFrame.getContentPane().add(myPanel);
        myFrame.getContentPane().add(BorderLayout.EAST,rightbutton);
        myFrame.getContentPane().add(BorderLayout.NORTH,upbutton);
        myFrame.getContentPane().add(BorderLayout.SOUTH,downbutton);
        myFrame.getContentPane().add(BorderLayout.WEST,leftbutton);
    }
        class rightListener implements ActionListener {
            public void actionPerformed(ActionEvent event) {
                rightbutton.setText("Pushed");
            }
        }
        class leftListener implements ActionListener {
            public void actionPerformed(ActionEvent event) {
                leftbutton.setText("Pushed");
            }
        }
        class downListener implements ActionListener {
            public void actionPerformed(ActionEvent event) {
                downbutton.setText("Pushed");
            }
        }
        class upListener implements ActionListener {
            public void actionPerformed(ActionEvent event) {
                upbutton.setText("Pushed");
            }
        }
}

4 つのボタンのいずれかを押したときに表示されるすべての例外を次に示します。

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at gui$leftListener.actionPerformed(gui.java:46)
    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$200(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)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at gui$upListener.actionPerformed(gui.java:56)
    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$200(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)
4

1 に答える 1

2

あなたの問題は、を宣言しているがJButtons、それらのために予約されているインスタンス変数にそれらを入れていないことです。次のような行を置き換えることでこれを修正できます。

JButton rightbutton = new JButton("Right Button");

と:

rightbutton = new JButton("Right Button");

最初の形式は、インスタンス変数を覆い隠すローカル変数を宣言するため、インスタンス変数は消えます。null ポインター例外は、リスナーがインスタンス変数にアクセスしようとしたときに発生します。これは、初期化されていないため、実際には null です。

この問題を解決する別の方法はevent.getSource()、インスタンス変数を使用する代わりに を使用することです。この場合、インスタンス変数は不要になります。

于 2013-08-01T22:49:25.840 に答える