线程中的异常" AWT-EventQueue-0"简单计算器(JAVA)

时间:2015-03-26 22:33:26

标签: java swing awt

我试图在java中做一个简单的计算器。它只需要考虑基本的四个操作。我认为逻辑是正确的,但是当我运行它时,我得到以下错误:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at simpleCalc$1equalsListener.actionPerformed(simpleCalc.java:118)
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)

有人可以帮帮我吗?我已经对这个错误消息做了一些研究,但是无法缩小我在代码中的问题。这是我的代码:

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class simpleCalc extends JFrame {
    /**
     * 
     */
    private boolean firstNum = true;
    private String numstr1 = " ";
    private String numstr2 = " ";
    private double num1;
    private double num2;
    private String opused;
    private double ans;
    private String finalans;
    public static void main(String[] args) {
        simpleCalc window = new simpleCalc();
    }

public simpleCalc() {

    setSize(400, 400);
    setTitle("Calculator");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    int width = 10;
    JPanel panel = new JPanel();
    JTextField textField = new JTextField(width);
    Container contentPane = getContentPane();
    contentPane.setLayout(new BorderLayout());
    panel.add(textField);
    contentPane.add(panel, BorderLayout.NORTH);

    String[] calcnumbers = { "0", "1", "2", "3", "4", "5", "6", "7", "8",
            "9" };
    JPanel numpanel = new JPanel();// number for calc start point
    numpanel.setLayout(new GridLayout(2, 2));

    for (int i = 0; i < calcnumbers.length; i++) {
        JButton calcButton = new JButton(calcnumbers[i]);
        class cbListener implements ActionListener {
            public void actionPerformed(ActionEvent event) {
                JButton thebutton = (JButton) event.getSource();
                if (firstNum) {
                    String currentText = textField.getText();
                    String newText = currentText + thebutton.getText();
                    textField.setText(newText);
                    numstr1 = numstr1 + thebutton.getText();
                } else {
                    textField.setText(" ");
                    String currentText = textField.getText();
                    String newText = currentText + thebutton.getText();
                    textField.setText(newText);
                    numstr2 = numstr2 + thebutton.getText();

                }
            }

        }

        cbListener cb = new cbListener();
        calcButton.addActionListener(cb);// end calcbuttons
        numpanel.add(calcButton);
        contentPane.add(numpanel, BorderLayout.CENTER);

    }

    String[] calcoperators = { "*", "+", "-", "/", };

    JPanel oppanel = new JPanel();
    oppanel.setLayout(new GridLayout(2, 2));
    for (int c = 0; c < calcoperators.length; c++) {
        JButton opButton = new JButton(calcoperators[c]);
        oppanel.add(opButton);
        contentPane.add(oppanel, BorderLayout.EAST);
        class opListener implements ActionListener {
            public void actionPerformed(ActionEvent event) {
                JButton theOP = (JButton) event.getSource();
                String opused = theOP.getText();
                firstNum = false;

                // String currentText = textField.getText();
                // String newText = currentText + theOP.getText();
                // textField.setText(newText);

            }
        }
        opListener ob = new opListener();
        opButton.addActionListener(ob);

    }
    JButton clear = new JButton("C");
    oppanel.add(clear);
    class clearListener implements ActionListener {
        public void actionPerformed(ActionEvent event) {
            JButton theClear = (JButton) event.getSource();
            textField.setText("");
        }
    }
    clearListener cl = new clearListener();
    clear.addActionListener(cl);

    JButton equals = new JButton("=");
    oppanel.add(equals);
    class equalsListener implements ActionListener {
        public void actionPerformed(ActionEvent event) {
            JButton theEquals = (JButton) event.getSource();
            textField.setText(" ");
            num1 = Double.parseDouble(numstr1);
            num2 = Double.parseDouble(numstr2);
            if (opused.equals("*")){
                ans = (num1 * num2 );
            }
            else if (opused.equals("/")){
                ans = (num1 / num2);
            }
            else if (opused.equals("+")){
                ans = (num1 + num2);
            }
            else if (opused.equals("-")){
                ans = (num1 - num2);
            }

            textField.setText(Double.toString(ans));
        }
    }
    equalsListener el = new equalsListener();
    equals.addActionListener(el);
    pack();
    setVisible(true);

}

}

1 个答案:

答案 0 :(得分:0)

您没有检查opused是否已设置,因为它未初始设置,初始值为null

你应该:

  • 检查opuser(推荐)
  • equalsListener.actionPerformed是否为空
  • 在类初始化
  • 中将opuser设置为空字符串

此时您还应该检查为什么opuser可能是null 可能的情况是用户输入一个数字并按= 而不按下operator按钮(这是一个有效的方案)。