Java中的奇怪运行时错误

时间:2016-08-28 18:28:10

标签: java swing nullpointerexception

我有一个简单的程序,可以获取有关购买商品的一些信息并增加总费用。我在运行时遇到了一些错误,我认为这与我创建按钮的方式有关。

swagger: "2.0"

info:
  title: A dummy title
  version: 1.0.0

paths:
  /competition:
    post:
      parameters:
        - name: labelValue
          in: body
          schema:
            $ref: '#/definitions/LabelValueObject'
      responses:
        default:
          description: Error
          schema:
            $ref: "#/definitions/AnyResponse"

definitions:
  AnyResponse:
    properties:
      any:
        type: string
  LabelValueObject:
    properties:
      label:
        type: string
      value:
        type: string
    required:
      - label
      - value

以下是错误消息:

    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); 
           }
     }   
}

不确定该怎么做^所以任何帮助都表示赞赏。谢谢!

0 个答案:

没有答案