" AWT-EventQueue的-0"带有ActionListener的java.lang.NullPointerException

时间:2014-09-16 14:00:48

标签: java actionlistener actionevent

我在尝试使用ActionListener调用另一个类中的方法时遇到此错误:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at MainProgram.actionPerformed(MainProgram.java:83)
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)

我发现这个错误的大部分解决方案都是该行中的某些内容为空,据我所知,情况并非如此(尽管我可能错了)。

如果点击'button1'(我知道,不良做法),它应该为Jim的现金加100,并将JTextField'jimBalance'更新为新值。

我放在那里的任何其他代码都可以正常工作,直到我从另一个类调用一个方法,即使它只是一行返回true或其他东西。

错误行(83)是

jim.GiveCash(100.0, jimBalance);

在actionPerformed方法中行。

主类:

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class MainProgram extends JFrame implements ActionListener{
    private JLabel label1;
    private JLabel label2;
    private static JTextField jimBalance;
    private static JTextField bobBalance;
    private JButton button1;
    private JButton button2;
    private JTextField jimPayment;
    private JTextField bobPayment;
    private Guy jim;
    private Guy bob;

    public static void main(String[] args) {
        MainProgram mainWindow;
        mainWindow = new MainProgram();
        mainWindow.init();
        mainWindow.setSize(400,110);
        mainWindow.setVisible(true);

        Guy jim = new Guy();
        Guy bob = new Guy();

        jim.ReceiveCash(200, jimBalance);


        jimBalance.setText(String.valueOf(jim.cash()));
        bobBalance.setText(String.valueOf(bob.cash()));


    }

    public void init(){
        Container pane = this.getContentPane();
        pane.setLayout(null);
        label1 = new JLabel();
        label1.setBounds(10,10,50,20);
        label1.setText("Jim");
        pane.add(label1);
        label2 = new JLabel();
        label2.setBounds(10,40,50,20);
        label2.setText("Bob");
        pane.add(label2);

        jimBalance = new JTextField();
        jimBalance.setBounds(50,10,100,20);
        jimBalance.setEditable(false);
        pane.add(jimBalance);
        bobBalance = new JTextField();
        bobBalance.setBounds(50,40,100,20);
        bobBalance.setEditable(false);
        pane.add(bobBalance);

        button1 = new JButton();
        button1.setBounds(160,10,100,20);
        button1.setText("Give Bob");
        pane.add(button1);
        button2 = new JButton();
        button2.setBounds(160,40,100,20);
        button2.setText("Give Jim");
        pane.add(button2);

        button1.addActionListener(this);

        jimPayment = new JTextField();
        jimPayment.setBounds(270,10,100,20);
        pane.add(jimPayment);
        bobPayment = new JTextField();
        bobPayment.setBounds(270,40,100,20);
        pane.add(bobPayment);
    }

    public void actionPerformed(ActionEvent e){
        jim.GiveCash(100.0, jimBalance);
    }
}

盖伊班:

import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class Guy {
    public String Name;
    public double cash;
    public void GiveCash(double amount, JTextField balance)
    {
        if((cash - amount) >= 0){
            cash = cash - amount;
            balance.setText(String.valueOf(cash));
        }else{
            JOptionPane.showMessageDialog(null, "You cannot afford that transaction!", "Error", JOptionPane.WARNING_MESSAGE);
        }
    }

    public void ReceiveCash(double amount, JTextField balance) { 
        cash = cash + amount;
        balance.setText(String.valueOf(cash));
    }
}

1 个答案:

答案 0 :(得分:0)

在下面我展示你的问题:

在(1)中定义对象变量jim

在(2)中,您在静态上下文中定义局部变量。

在(3)jim仍为null,因为这是一个未初始化的对象变量,您无法从静态上下文初始化(如main()方法)。

public class MainProgram extends JFrame implements ActionListener {
// Omit some code
private Guy jim; // (1)
private Guy bob;

public static void main(String[] args) {
    // Omit some code

    Guy jim = new Guy(); // (2)
    Guy bob = new Guy();

    jim.ReceiveCash(200, jimBalance);

    // Omit some code
}

// Omit some code

public void actionPerformed(ActionEvent e) {
    jim.GiveCash(100.0, jimBalance); // (3)
}

}