我正在编写一个比萨订购服务程序,带有复选框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);
}
}
}
}