带有复选框和按钮的空指针异常

时间:2013-06-02 21:40:53

标签: java swing checkbox nullpointerexception

我正在编写一个比萨饼订购服务程序,带有复选框radioButtons,因此您可以为比萨饼选择不同的选项,然后在您单击OrderPizza按钮时读取价格。每次我点击OrderPizza按钮,我都会得到一个NullPointerException。它说的问题在线

pPrice = stylePrice + sizePrice + sPrice + cPrice + tPrice;

我不知道如何解决这个问题并且无法找到任何地方,所以任何帮助都会很棒。谢谢!

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

public class Pizza extends JPanel
{
private double pPrice;
private JRadioButton small;
private JRadioButton medium;
private JRadioButton large;
private JRadioButton xlarge;
private JRadioButton NY;
private JRadioButton NP;
private JRadioButton CD;
private JRadioButton WW;
private JCheckBox Mozzarella;
private JCheckBox Parmesan;
private JCheckBox Goat;
private JCheckBox Feta;
private JCheckBox Tomato;
private JCheckBox Pesto;
private JCheckBox BBQ;
private JCheckBox Pepperoni;
private JCheckBox Anchovies;
private JCheckBox Olives;
private JCheckBox Basil;
private JCheckBox SDT;
private JCheckBox Sausage;
private JCheckBox Peppers;
private JCheckBox Vegetarian;
private JCheckBox Hawaiian;
private JCheckBox ML;
private JLabel PizzaNumber;
private JLabel PizzaTotal;
private int count;
private JPanel Totals;
private JButton OrderPizza;
private double sizePrice;
private double stylePrice;
private double cPrice;
private double tPrice;
private double sPrice;



public Pizza()
{
  pPrice = 0;
  count = 0;
  sizePrice=0;
  stylePrice=0;
  cPrice=0;
  tPrice=0;
  sPrice=0;
  setLayout (new GridLayout(6,1));

  JPanel size = new JPanel(); 
  JPanel basic = new JPanel();
  JPanel sauce = new JPanel();
  JPanel cheese = new JPanel();
  JPanel toppings = new JPanel();
  JPanel crustPanel = new JPanel();
  JPanel Totals = new JPanel();



  //size

  TitledBorder SizeTitle;
  SizeTitle = BorderFactory.createTitledBorder("Size");

  ButtonGroup PizzaSize = new ButtonGroup();

  small = new JRadioButton("Small: $10");
  medium = new JRadioButton("Medium: $13");
  large = new JRadioButton("Large: $16");
  xlarge = new JRadioButton("Extra Large: $20");


  PizzaSize.add(small);
  PizzaSize.add(medium);
  PizzaSize.add(large);
  PizzaSize.add(xlarge);


  SizeListener listener1 = new SizeListener();
  small.addActionListener (listener1);
  medium.addActionListener (listener1);
  large.addActionListener (listener1);
  xlarge.addActionListener (listener1);

  size.add(small);
  size.add(medium);
  size.add(large);
  size.add(xlarge);


  size.setBorder(SizeTitle);
  add(size);


  //style

  TitledBorder crust;
  crust = BorderFactory.createTitledBorder("Crust");

  crustPanel.setLayout (new GridLayout(4,1));

  ButtonGroup Style = new ButtonGroup();

  NY = new JRadioButton("New York: $0");
  NP = new JRadioButton("Neopolitan: $3");
  CD = new JRadioButton("Chicago Deep Dish: $5");
  WW = new JRadioButton("Whole Wheat: $5");

  Style.add(NY);
  Style.add(NP);
  Style.add(CD);
  Style.add(WW);


  StyleListener listener2 = new StyleListener();
  NY.addActionListener (listener2);
  NP.addActionListener (listener2);
  CD.addActionListener (listener2);
  WW.addActionListener (listener2);


  crustPanel.add(NY);
  crustPanel.add(NP);
  crustPanel.add(CD);
  crustPanel.add(WW);

  crustPanel.setBorder(crust);

  basic.add(crustPanel);




  //sauce

  TitledBorder SauceTitle;
  SauceTitle = BorderFactory.createTitledBorder("Sauce");
  sauce.setBorder(SauceTitle);

  sauce.setLayout (new GridLayout(4,1));

  Tomato = new JCheckBox("Tomato: $0");
  Pesto = new JCheckBox("Pesto: $2");
  BBQ = new JCheckBox("Barbeque: $2");


  SauceListener listener3 = new SauceListener();
  Tomato.addItemListener (listener3);
  Pesto.addItemListener (listener3);
  BBQ.addItemListener (listener3);

  sauce.add(Tomato);
  sauce.add(Pesto);
  sauce.add(BBQ);

  basic.add(sauce);


  //cheese

  TitledBorder CheeseTitle;
  CheeseTitle = BorderFactory.createTitledBorder("Cheese");
  cheese.setBorder(CheeseTitle);

  cheese.setLayout (new GridLayout(4,1));

  Mozzarella = new JCheckBox("Mozzarella: $0");
  Parmesan = new JCheckBox("Parmesan: $.50");
  Goat = new JCheckBox("Goat Cheese: $1");
  Feta = new JCheckBox("Feta: $1");



  CheeseListener listener4 = new CheeseListener();
  Mozzarella.addItemListener (listener4);
  Parmesan.addItemListener (listener4);
  Goat.addItemListener (listener4);
  Feta.addItemListener (listener4);

  cheese.add(Mozzarella);
  cheese.add(Parmesan);
  cheese.add(Goat);
  cheese.add(Feta);

  basic.add(cheese);



  add(basic);

  //toppings

  TitledBorder ToppingTitle;
  ToppingTitle = BorderFactory.createTitledBorder("Toppings: $.75 " +
      "per topping");
  toppings.setBorder(ToppingTitle);

  toppings.setLayout (new GridLayout(2,5));

  Pepperoni = new JCheckBox("Pepperoni");
  Anchovies = new JCheckBox("Anchovies");
  Basil = new JCheckBox("Basil");
  Olives = new JCheckBox("Olives");
  SDT = new JCheckBox("Sun Dried Tomatoes");
  Sausage = new JCheckBox("Sausage");
  Peppers = new JCheckBox("Peppers");
  Hawaiian = new JCheckBox("Hawaiian");
  Vegetarian = new JCheckBox("Vegetarian");
      ML = new JCheckBox("Meat Lover's (Pepperoni and Sausage)");

      ToppingsListener listener5 = new ToppingsListener();
      Pepperoni.addItemListener (listener5);
      Anchovies.addItemListener (listener5);
      Basil.addItemListener (listener5);
      Olives.addItemListener (listener5);
      SDT.addItemListener (listener5);
      Sausage.addItemListener (listener5);
      Peppers.addItemListener (listener5);
      Hawaiian.addItemListener (listener5);
      Vegetarian.addItemListener (listener5);
      ML.addItemListener (listener5);


      toppings.add(Pepperoni);
      toppings.add(Anchovies);
      toppings.add(Basil);
      toppings.add(Olives);
      toppings.add(SDT);
      toppings.add(Sausage);
      toppings.add(Peppers);
      toppings.add(Hawaiian);
      toppings.add(Vegetarian);
      toppings.add(ML);

      add(toppings);


  OrderPizza = new JButton("Order This Pizza");


  ButtonListener listener6 = new ButtonListener();
  OrderPizza.addActionListener (listener6);


  add(OrderPizza);


  JLabel PizzaTotal = new JLabel("Price of Pizzas: " + pPrice);
  JLabel PizzaNumber = new JLabel("Number of Pizzas " + count);

  Totals.setLayout (new GridLayout(2,1));
  Totals.add(PizzaTotal);
  Totals.add(PizzaNumber);

  add(Totals);


}


  private class SizeListener implements ActionListener
  {
     public void actionPerformed (ActionEvent event1)
     {
             Object source = event1.getSource();

             if (source == small)
                     sizePrice += 10;
             else if (source == medium)
                     sizePrice += 13;
             else if (source == large)
                     sizePrice +=16;
             else 
                     sizePrice += 20;
     }
   }


  private class StyleListener implements ActionListener
  {
     public void actionPerformed (ActionEvent event2)
     {
             Object source = event2.getSource();

             if (source == NY)
                     stylePrice += 0;
             else if (source == NP)
                     stylePrice += 3;
             else if (source == CD)
                     stylePrice += 5;
             else 
                     stylePrice += 5;
     }
   }

   private class SauceListener implements ItemListener
   {
       public void  itemStateChanged (ItemEvent event2)
       {
           if (Pesto.isSelected())
               sPrice += 2;
           if (BBQ.isSelected())
               sPrice +=2;
       }
   }

   private class CheeseListener implements ItemListener
   {
       public void  itemStateChanged (ItemEvent event3)
       {
           if (Parmesan.isSelected())
               cPrice += .5;
           if (Goat.isSelected())
               cPrice +=1;
           if (Feta.isSelected())
               cPrice +=1;
       }
   }

   private class ToppingsListener implements ItemListener
   {
       public void  itemStateChanged (ItemEvent event4)
       {
           if (Pepperoni.isSelected())
               tPrice += .75;
           if (Anchovies.isSelected())
               tPrice += .75;
           if (Basil.isSelected())
               tPrice += .75;
           if (Olives.isSelected())
               tPrice += .75;
           if (SDT.isSelected())
               tPrice += .75;
           if (Sausage.isSelected())
               tPrice += .75;
           if (Peppers.isSelected())
               tPrice += .75;
           if (Hawaiian.isSelected())
               tPrice += .75;
           if (Vegetarian.isSelected())
               tPrice += .75;
           if (ML.isSelected())
               tPrice += .75;

       }
   }

   private class ButtonListener implements ActionListener
   {
       public void actionPerformed (ActionEvent event5)
       {
         if (event5.getSource() == OrderPizza)
         {
           count++;
       pPrice = stylePrice + sizePrice + sPrice + cPrice + tPrice;
           PizzaNumber.setText("Number of Pizzas" + count);
           PizzaTotal.setText("Pizza Price" + pPrice);

         }

       }
   }

}

1 个答案:

答案 0 :(得分:5)

阅读stacktrace:

您正在PizzaTotal的构造函数中隐藏PizzaNumberPizza个变量。因此,同名的全局成员变量保持未分配状态。 对变量的任何尝试操作都将导致NPE中的ActionListener。取代

JLabel PizzaTotal = new JLabel("Price of Pizzas: " + pPrice);
JLabel PizzaNumber = new JLabel("Number of Pizzas " + count);

PizzaTotal = new JLabel("Price of Pizzas: " + pPrice);
PizzaNumber = new JLabel("Number of Pizzas " + count);

除此之外:Java命名约定表明变量名称以小写字母开头,例如pizzaTotal。阅读它们here