我正在尝试用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);
}
}
您没有检查是否设置了opused
,因为它不是初始设置的,初始值是null
。
您应该:
- 检查
equalsListener.actionPerformed
中的opuser
是否为空(推荐) - 在类初始化时将
opuser
设置为空字符串
您还应该检查为什么此时opuser
可能是null
可能的情况是,用户输入一个数字并按下=
,而不按下operator
按钮(这是一种有效的情况)。