我正在为披萨餐厅编写程序,代码必须计算总价 根据客户对披萨的要求。
代码
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JOptionPane;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.*;
class Piz
{
Double [] small1 ;
Double [] medium1 ;
Double [] larg1 ;
Double [] tomato1 ;
Double [] olive1 ;
Double [] chicken1 ;
Double [] meat1 ;
Double [] hotdog1 ;
Double [] mashrom1;
Double [] onion1 ;
public Piz ()
{
small1 = new Double [2];
medium1 = new Double [2];
larg1= new Double [2];
tomato1= new Double [3];
olive1 = new Double [3];
chicken1 = new Double [3];
meat1 = new Double [3];
hotdog1 = new Double [3];
mashrom1 = new Double [3];
onion1 = new Double [3];
small1[0] = 1.5 ;
small1[1] = 1.4 ;
medium1[0] = 2.5 ;
medium1[1] = 2.4 ;
larg1[0] = 3.5 ;
larg1[1] = 3.4 ;
tomato1[0] = 0.2 ;
tomato1[1] = 0.3 ;
tomato1[2] = 0.4 ;
olive1[0] = 0.2 ;
olive1[1] = 0.3 ;
olive1[2] = 0.4 ;
chicken1[0] = 0.3 ;
chicken1[1] = 0.4 ;
chicken1[2] = 0.5 ;
hotdog1[0] = 0.3 ;
hotdog1[1] = 0.4 ;
hotdog1[2] = 0.5 ;
meat1[0] = 0.3 ;
meat1[1] = 0.4 ;
meat1[2] = 0.5 ;
mashrom1[0] = 0.2 ;
mashrom1[1] = 0.3 ;
mashrom1[2] = 0.4 ;
onion1[0] = 0.2 ;
onion1[1] = 0.3 ;
onion1[2] = 0.4 ;
}
}
class PatientFrame extends JFrame
{
private Piz p;
private ButtonGroup radioGroup1;
private ButtonGroup radioGroup2;
private JLabel size ;
private JLabel topping ;
private JLabel crust ;
private JLabel pizzrio;
private JButton add ;
private JButton cancel;
private JButton print;
private JRadioButton small ;
private JRadioButton medium ;
private JRadioButton larg ;
private JRadioButton thick;
private JRadioButton thin;
private JCheckBox tomato ;
private JCheckBox olives ;
private JCheckBox chicken ;
private JCheckBox meat ;
private JCheckBox hotdog ;
private JCheckBox mashroom ;
private JCheckBox onion ;
private JTextArea text1 ;
private ImageIcon icon ;
public PatientFrame()
{
super ("p i Z z R i O");
add = new JButton ("Add");
setLayout (new FlowLayout());
size = new JLabel ("Size");
topping = new JLabel ("Topping");
crust = new JLabel ("Crust");
small = new JRadioButton ("Small" ,true);
medium = new JRadioButton ("Medium", false);
larg = new JRadioButton ("Larg" , false);
radioGroup1 = new ButtonGroup();
radioGroup1.add (small);
radioGroup1.add (medium);
radioGroup1.add (larg);
thick = new JRadioButton ("Thick" ,true);
thin = new JRadioButton ("Thin" ,false);
radioGroup2 = new ButtonGroup();
radioGroup2.add (thick);
radioGroup2.add (thin);
tomato = new JCheckBox ("Tomato");
olives = new JCheckBox ("Olives");
chicken = new JCheckBox ("Chicken");
meat = new JCheckBox ("Meat");
hotdog = new JCheckBox ("Totdog");
mashroom = new JCheckBox ("Mashroom");
onion = new JCheckBox ("Onion");
Box box = Box.createHorizontalBox();
text1 = new JTextArea( 40 , 40);
box.add ( new JScrollPane(text1));
pizzrio = new JLabel ("p i Z z R i O");
pizzrio.setFont(new Font("Serif", Font.PLAIN, 14));
add (pizzrio);
add ( topping );
add ( crust );
add(small);
add(medium);
add(larg);
add(tomato);
add(olives);
add(chicken);
add(meat);
add(hotdog);
add(mashroom);
add (onion);
add(thick);
add(thin);
add(add);
add(box);
text1.setText(" Size Topping Crust Price");
EventHandler han = new EventHandler() ;
add.addActionListener(han);
}
private class EventHandler implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
String [] toppinglist = new String [20] ;
String s = "";
String c = "";
Double sizep = 0.0 ;
Double toppingp = 0.0 ;
int count = 0 ;
if (small.isSelected() && thick.isSelected())
{
sizep = p.small1[0];
s = small.getText() ;
c= "Thick";
if (tomato.isSelected())
{
toppingp = toppingp + p.tomato1[0];
toppinglist[count] = "Tomato";
count ++ ;
}
if (olives.isSelected())
{
toppingp = toppingp + p.olive1[0];
toppinglist[count] = "Olives";
count ++ ;
}
if (chicken.isSelected())
{
toppingp = toppingp + p.chicken1[0];
toppinglist[count] = "Chicken";
count ++ ;
}
if (meat.isSelected())
{
toppingp = toppingp + p.meat1[0];
toppinglist[count] = "Meat";
count ++ ;
}
if (hotdog.isSelected())
{
toppingp = toppingp + p.hotdog1[0];
toppinglist[count] = "Hotdog";
count ++ ;
}
if (mashroom.isSelected())
{
toppingp = toppingp + p.mashrom1[0];
toppinglist[count] = "Mashroom";
count ++ ;
}
if (onion.isSelected())
{
toppingp = toppingp + p.onion1[0];
toppinglist[count] = "Onion";
count ++ ;
}
}
if (small.isSelected() && thin.isSelected())
{
sizep = p.small1[1];
s = "Small" ;
c = "Thin";
if (tomato.isSelected())
{
toppingp = toppingp + p.tomato1[0];
toppinglist[count] = "Tomato";
count ++ ;
}
if (olives.isSelected())
{
toppingp = toppingp + p.olive1[0];
toppinglist[count] = "Olives";
count ++ ;
}
if (chicken.isSelected())
{
toppingp = toppingp + p.chicken1[0];
toppinglist[count] = "Chicken";
count ++ ;
}
if (meat.isSelected())
{
toppingp = toppingp + p.meat1[0];
toppinglist[count] = "Meat";
count ++ ;
}
if (hotdog.isSelected())
{
toppingp = toppingp + p.hotdog1[0];
toppinglist[count] = "Hotdog";
count ++ ;
}
if (mashroom.isSelected())
{
toppingp = toppingp + p.mashrom1[0];
toppinglist[count] = "Mashroom";
count ++ ;
}
if (onion.isSelected())
{
toppingp = toppingp + p.onion1[0];
toppinglist[count] = "Onion";
count ++ ;
}
}
if (medium.isSelected() && thick.isSelected())
{
sizep = p.medium1[0];
s = "Medium";
c = "Thick" ;
if (tomato.isSelected())
{
toppingp = toppingp + p.tomato1[1];
toppinglist[count] = "Tomato";
count ++ ;
}
if (olives.isSelected())
{
toppingp = toppingp + p.olive1[1];
toppinglist[count] = "Olives";
count ++ ;
}
if (chicken.isSelected())
{
toppingp = toppingp + p.chicken1[1];
toppinglist[count] = "Chicken";
count ++ ;
}
if (meat.isSelected())
{
toppingp = toppingp + p.meat1[1];
toppinglist[count] = "Meat";
count ++ ;
}
if (hotdog.isSelected())
{
toppingp = toppingp + p.hotdog1[1];
toppinglist[count] = "Hotdog";
count ++ ;
}
if (mashroom.isSelected())
{
toppingp = toppingp + p.mashrom1[1];
toppinglist[count] = "Mashroom";
count ++ ;
}
if (onion.isSelected())
{
toppingp = toppingp + p.onion1[1];
toppinglist[count] = "Onion";
count ++ ;
}
}
if (medium.isSelected() && thin.isSelected())
{
sizep = p.medium1[1];
s = "Medium";
c = "Thin" ;
if (tomato.isSelected())
{
toppingp = toppingp + p.tomato1[1];
toppinglist[count] = "Tomato";
count ++ ;
}
if (olives.isSelected())
{
toppingp = toppingp + p.olive1[1];
toppinglist[count] = "Olives";
count ++ ;
}
if (chicken.isSelected())
{
toppingp = toppingp + p.chicken1[1];
toppinglist[count] = "Chicken";
count ++ ;
}
if (meat.isSelected())
{
toppingp = toppingp + p.meat1[1];
toppinglist[count] = "Meat";
count ++ ;
}
if (hotdog.isSelected())
{
toppingp = toppingp + p.hotdog1[1];
toppinglist[count] = "Hotdog";
count ++ ;
}
if (mashroom.isSelected())
{
toppingp = toppingp + p.mashrom1[1];
toppinglist[count] = "Mashroom";
count ++ ;
}
if (onion.isSelected())
{
toppingp = toppingp + p.onion1[1];
toppinglist[count] = "Onion";
count ++ ;
}
}
if (larg.isSelected() && thick.isSelected())
{
sizep = p.larg1[0];
s = "Larg";
c = "Thick" ;
if (tomato.isSelected())
{
toppingp = toppingp + p.tomato1[2];
toppinglist[count] = "Tomato";
count ++ ;
}
if (olives.isSelected())
{
toppingp = toppingp + p.olive1[2];
toppinglist[count] = "Olives";
count ++ ;
}
if (chicken.isSelected())
{
toppingp = toppingp + p.chicken1[2];
toppinglist[count] = "Chicken";
count ++ ;
}
if (meat.isSelected())
{
toppingp = toppingp + p.meat1[2];
toppinglist[count] = "Meat";
count ++ ;
}
if (hotdog.isSelected())
{
toppingp = toppingp + p.hotdog1[2];
toppinglist[count] = "Hotdog";
count ++ ;
}
if (mashroom.isSelected())
{
toppingp = toppingp + p.mashrom1[2];
toppinglist[count] = "Mashroom";
count ++ ;
}
if (onion.isSelected())
{
toppingp = toppingp + p.onion1[2];
toppinglist[count] = "Onion";
count ++ ;
}
}
if (larg.isSelected() && thin.isSelected())
{
sizep = p.larg1[1];
s = "Larg";
c = "Thin" ;
if (tomato.isSelected())
{
toppingp = toppingp + p.tomato1[2];
toppinglist[count] = "Tomato";
count ++ ;
}
if (olives.isSelected())
{
toppingp = toppingp + p.olive1[2];
toppinglist[count] = "Olives";
count ++ ;
}
if (chicken.isSelected())
{
toppingp = toppingp + p.chicken1[2];
toppinglist[count] = "Chicken";
count ++ ;
}
if (meat.isSelected())
{
toppingp = toppingp + p.meat1[2];
toppinglist[count] = "Meat";
count ++ ;
}
if (hotdog.isSelected())
{
toppingp = toppingp + p.hotdog1[2];
toppinglist[count] = "Hotdog";
count ++ ;
}
if (mashroom.isSelected())
{
toppingp = toppingp + p.mashrom1[2];
toppinglist[count] = "Mashroom";
count ++ ;
}
if (onion.isSelected())
{
toppingp = toppingp + p.onion1[2];
toppinglist[count] = "Onion";
count ++ ;
}
}
String sizepp = String.valueOf(sizep);
if (event.getSource() == add)
text1.setText(sizepp);
}
}
}
public class Ass03
{
public static void main (String [] args)
{
PatientFrame patientFrame1 = new PatientFrame();
patientFrame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
patientFrame1.setSize(700,304);
patientFrame1.setVisible(true);
}
}
堆栈跟踪
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at PatientFrame$EventHandler.actionPerformed(Ass03.java:213)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
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:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:729)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:688)
at java.awt.EventQueue$3.run(EventQueue.java:686)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:702)
at java.awt.EventQueue$4.run(EventQueue.java:700)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:699)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
我希望你能帮助我找出错误的来源
答案 0 :(得分:2)
堆栈跟踪的重要一行是:
at PatientFrame$EventHandler.actionPerformed(Ass03.java:213)
基本上,它告诉您第213行有NULL
个对象。
假设您已粘贴整个代码,则它对应于以下行:
sizep = p.small1[0];
您正在尝试获取small1
的{{1}}属性,该属性未分配。我找不到p
变量的任何作业。也许你在构造函数中错过了它?