I am working on a simple calculator program using java and javax.swing
Basically, when you press a button, the program should get the function of that button (number or operation) and display it in the text area.
All logic for the calculator itself does not matter. In addition, there is a clear menu item that clears all text in textArea.
However, every time I press the button, I get the following error:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at calculator.CalculatorGUI.actionPerformed (CalculatorGUI.java:106) '
I also get a similar error when I press the clear button , it seems that Java does not like it when I want to change the text in the text area.
Here is my code: ( line 106 is in the method actionPerfomed, I marked it for your convenience)
public class CalculatorGUI implements ActionListener
{
private JFrame frame;
private JMenuBar menuBar;
private JTextArea textArea;
private JScrollPane scrollArea;
private JPanel numKeysPane;
private JPanel opKeysPane;
private String input;
final String[] numbers = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "0" };
final String[] operations = { "+", "-", "*", "/" };
public CalculatorGUI() {
calculator = new RPNCalculator();
}
public void showCalculator() {
String buttonValue;
JButton button;
JMenu menu;
JMenuItem menuItem;
frame = new JFrame("RPN Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
numKeysPane = new JPanel(new GridLayout(4, 3));
opKeysPane = new JPanel(new GridLayout(2, 2));
initializeMenu();
initializeNumberPad();
initializeOps();
JTextArea textArea = new JTextArea();
textArea.setEditable(false);
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
JScrollPane scrollArea = new JScrollPane(textArea);
frame.getContentPane().add(numKeysPane, BorderLayout.CENTER);
frame.getContentPane().add(scrollArea, BorderLayout.NORTH);
frame.getContentPane().add(opKeysPane, BorderLayout.LINE_END);
frame.pack();
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String s = (String)e.getActionCommand();
textArea.append(s + " ");
}
private void initializeNumberPad() {
JButton button;
for (int i = 0; i < numbers.length; i++) {
button = new JButton(numbers[i]);
button.addActionListener(this);
numKeysPane.add(button);
}
}
private void initializeOps(){
JButton button;
for (int i = 0; i < operations.length; i++){
button = new JButton(operations[i]);
button.addActionListener(this);
opKeysPane.add(button);
}
}
private void initializeMenu() {
JMenu menu;
JMenuItem menuItem;
JMenuItem menuItem2;
menu = new JMenu("Calculator");
menuItem = new JMenuItem("Quit");
menuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.err.println("Close window");
frame.setVisible(false);
frame.dispose();
}
});
menuItem2 = new JMenuItem("Clear");
menuItem2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
textArea.setText("");
}
});
menu.add(menuItem2);
menu.add(menuItem);
menuBar = new JMenuBar();
menuBar.add(menu);
frame.setJMenuBar(menuBar);
}
private static void errorPopup(String message) {
JOptionPane.showMessageDialog(null, message);
}
}
Any help would be greatly appreciated! Thank!