0

I have a simple program that takes in some information about purchased items and adds the total cost. I am getting some errors during runtime, which I think have something to do with the way in which I created the buttons.

    import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/*
    Day 3 - Lab 1
*/

public class Orders extends JFrame implements ActionListener
{

   private JTextField itemName;
   private JTextField numberOf;
   private JTextField cost;
   private JTextField amountOwed;

   private JButton jbCalc;
   private JButton jbSave;
   private JButton jbClear;
   private JButton jbExit;

   // Main class
   public static void main(String [] args)
   {
      Orders create = new Orders();
   }

   // Constructor
   public Orders()
   {
      //Content panel
      JPanel content = new JPanel(new GridLayout(4,2) );

          //Add labels and text fields to that panel
          content.add( new JLabel("Item Name:") );
          JTextField itemName = new JTextField(5);
          content.add( itemName );

          content.add( new JLabel("Number of:") );
          JTextField numberOf = new JTextField(5);
          content.add( numberOf );

          content.add( new JLabel("Cost:") );
          JTextField cost = new JTextField(5);
          content.add( cost );

          content.add( new JLabel("Amount owed:") );
          JTextField amountOwed = new JTextField(5);
          content.add( amountOwed );
          amountOwed.setEnabled(false);


      add( content, BorderLayout.CENTER);

      //Control panel
      JPanel control = new JPanel();

         // Add the buttons
         jbCalc = new JButton("Calculate");
         control.add( jbCalc );
         jbCalc.addActionListener( this );

         // button - Save
         jbSave = new JButton("Save");
         control.add( jbSave );
         jbSave.addActionListener( this );

         // button - Clear
         jbClear = new JButton("Clear");
         control.add( jbClear );
         jbClear.addActionListener( this );

         // button - Exit
         jbExit = new JButton("Exit");
         control.add( jbExit );
         jbExit.addActionListener( this );

         add( control, BorderLayout.SOUTH );

       // Show it
       pack();
       setLocationRelativeTo(null);
       setVisible(true);
    }

    // Actions of Buttons
    @Override
    public void actionPerformed(ActionEvent a)
    {
      Object choice = a.getSource();

         // Button - Calculate
         if ( choice == jbCalc )
           {
             System.out.println("Calculating...");
             String test = itemName.getText();
           }
         // Button - Save
         else if ( choice == jbSave )
           {
             System.out.println("Saving...");
           }
         // Button - Clear
         else if ( choice == jbClear )
           {
              System.out.println("Clearing...");
              itemName.setText("");
              numberOf.setText(null);
              cost.setText(null);
              amountOwed.setText(null);
           }

         // Button - Exit
         else if ( choice == jbExit )
           {
              System.out.println("Exiting...");
              System.exit(0); 
           }
     }   
}

And here are the error messages:

Clearing...
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at Orders.actionPerformed(Orders.java:107)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6533)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
    at java.awt.Component.processEvent(Component.java:6298)
    at java.awt.Container.processEvent(Container.java:2236)
    at java.awt.Component.dispatchEventImpl(Component.java:4889)
    at java.awt.Container.dispatchEventImpl(Container.java:2294)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
    at java.awt.Container.dispatchEventImpl(Container.java:2280)
    at java.awt.Window.dispatchEventImpl(Window.java:2746)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.awt.EventQueue$4.run(EventQueue.java:729)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Saving...
Exiting...

Not sure what to make of that ^ so any help is appreciated. Thanks!

  • you do setText(null) in your method for your clearing button – marts Aug 28 '16 at 18:31
  • @marts And why do you think this is the problem? Neither the Stacktrace nor the JavaDoc of `setText` are suggesting that. – Tom Aug 28 '16 at 18:33
  • The proper dupe is: [Why does Java throw NullPointerException here?](http://stackoverflow.com/q/30567802) (it explains what the exact problem here is) – Tom Aug 28 '16 at 18:34
  • The link provide by Tom above is a much better link to follow than the one provided by Oliver when he closed the question. – camickr Aug 28 '16 at 18:39
  • @camickr The link he used is the common allrounder for this case :D. Can you reclose this question, since you have a gold badge? – Tom Aug 28 '16 at 18:44

0 Answers0