title: Exception in thread "AWT-EventQueue-0" simple calculator(JAVA)
body: <p>I am attempting to do a simple calculator in java. It only has to account for the basic four operations. I think the logic is correct, however when I run it I get the following error:</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>Can someone please help me? Ive done some research on this error message but cant narrow down my problem in the code. Here is my code:</p>
  
  <pre><code>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 &lt; 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 &lt; 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);
  
  }
  </code></pre>
  
  <p>}</p>