无法计算价格

时间:2015-02-03 11:49:43

标签: java

我目前正在订购系统,我遇到了一些问题,例如在我的面板中显示总价

public class SummaryPanel extends MasterPanel  {
private JTable table;
public JTable tblList3;
protected ArrayList<Food>foodList;
protected JComboBox category_CB;
protected JComboBox cb_FoodItem;
private JComboBox combo;

private JTextField ImagePath_TF;
private double total;
private String totalPrice;
private Orders orders;
private JComboBox comboBox;
private JComboBox cb;

private JComboBox cb_tableno;
private double totalAmount;
private ComboBoxModel cb_nopax1;
    private JComboBox cbNoPax;
 private String numPax;
 private String tableNo;
    private JLabel txtPrice;
 private Double s;
/**
 * Create the panel.
 */

 public SummaryPanel(JFrame myFrame, ArrayList<Food> listSelected) {

    super(myFrame);
    this.foodList =listSelected;
    setBackground(Color.WHITE);
    setForeground(Color.WHITE);;

    JLabel lblConfirmOrder = new JLabel("Confirm Order");
    lblConfirmOrder.setFont(new Font("Tw Cen MT", Font.PLAIN, 26));
    lblConfirmOrder.setBounds(52, 105, 173, 49);
    add(lblConfirmOrder);

    JScrollPane scrollPane = new JScrollPane();
    scrollPane.setBounds(27, 342,568, 204);
    add(scrollPane);


    tblList3 = new JTable();
    setTableModel();
    scrollPane.setViewportView(tblList3);




    JButton btnHome = new JButton("Cancle Order");
    btnHome.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            actionPerformedCancleOrder();

        }


    });
    btnHome.setBounds(93, 554, 119, 43);
    add(btnHome);

    JLabel lblTableNo = new JLabel("Table No");
    lblTableNo.setBounds(337, 241, 67, 39);
    add(lblTableNo);


    JButton btnOrder = new JButton("Order");
    btnOrder.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            actionPerformedOrder();
        }

    });
    btnOrder.setBounds(297, 554, 151, 43);
    add(btnOrder);

    JLabel lblNumberOfPax = new JLabel("Number Of Pax");
    lblNumberOfPax.setBounds(337, 174, 106, 27);
    add(lblNumberOfPax);



    /*JButton btnPrice = new JButton("Price");
    btnPrice.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            actionPerformedTotalPrice();
        }


    });
    btnPrice.setBounds(71, 176, 89, 23);
    add(btnPrice); */

    JLabel label = new JLabel("")label.setIcon(newImageIcon(SummaryPanel.class.getResource("/fourFinger/
 images/Wall-E6.jpg")));
    label.setBounds(636, 327, 253, 258);
    add(label);

    JLabel lblPleaseIndicateThe = new JLabel("Please indicate the  ");
    lblPleaseIndicateThe.setFont(new Font("Tw Cen MT", Font.BOLD, 19));
    lblPleaseIndicateThe.setBounds(561, 174, 173, 55);
    add(lblPleaseIndicateThe);

    JLabel lblTheSelectedFood = new JLabel("quantity of the");
    lblTheSelectedFood.setFont(new Font("Tw Cen MT", Font.BOLD, 19));
    lblTheSelectedFood.setBounds(561, 209, 173, 31);
    add(lblTheSelectedFood);

    JLabel lblFoodSelected = new JLabel("food selected");
    lblFoodSelected.setFont(new Font("Tw Cen MT", Font.BOLD, 19));
    lblFoodSelected.setBounds(561, 251, 119, 14);
    add(lblFoodSelected);

    JLabel lblDsdsds = new JLabel("");
    lblDsdsds.setIcon(new ImageIcon(SummaryPanel.class.getResource("/fourFinger/images/chatbox.png")));
    lblDsdsds.setBounds(527, 151, 237, 176);
    add(lblDsdsds);

    txtPrice = new JLabel("");

    txtPrice.setBounds(113, 177, 74, 21);
    add(txtPrice);


   cb_tableno = new JComboBox();
    cb_tableno.setBounds(424, 250, 93, 20);
    cb_tableno.setModel(new DefaultComboBoxModel(new String[]{ "1","2","3","4","5","6","7","8","9","10"}));
    add(cb_tableno);

     cbNoPax = new JComboBox();
    cbNoPax.setBounds(424, 177, 93, 20);
    cbNoPax .setModel(new DefaultComboBoxModel(new String[]{ "1","2","3","4","5","6","7","8","9","10"}));
    add(cbNoPax);


}


 private void actionPerformedOrder() {
 //retrieve user input
    String numPax = (String) cbNoPax.getSelectedItem();
    String tableNo= (String)cb_tableno.getSelectedItem();
   Date orderDate = new Date();

    orders=newOrders(Integer.parseInt(tableNo),Integer.parseInt(numPax),orderDate, totalAmount);
   int orderID = OrdersDA.createOrders(orders);

    }


    private double getTotalPrice(){   
        total = 0;
        for(Food fd:foodList){
     total += fd.getFoodPrice()*Food.getQuantity();
        System.out.printf("Total:$%.2f ", total);

    }

    return total;

}

    private void actionPerformedCancleOrder() {
    int resp = JOptionPane.showConfirmDialog(myFrame,  "Confirm Cancle Order \n\n Note : Selected food items will be discarded" , "Confirmation", JOptionPane.YES_NO_OPTION);
    if(resp == JOptionPane.NO_OPTION){

    }
    else {
        MasterPanel contentPane = new MasterPanel(myFrame);
        myFrame.setContentPane(contentPane);
        myFrame.setVisible(true);

    }


}
    private void setTableModel() {
    FoodTableModel model = new FoodTableModel(foodList);
    totalAmount = getTotalPrice();

    s = String.valueOf(totalAmount);
    txtPrice.setText(s);
    tblList3.setModel(model);
    tblList3.removeColumn(tblList3.getColumnModel().getColumn(0));


}
}

错误发生在:txtPrice.setText(s)并在运行问题时声明为NullPointerexception。谁知道哪里出了问题?

2 个答案:

答案 0 :(得分:3)

您在指令setTableModel()之前调用方法txtPrice = new JLabel("");。如果您想要更精确的答案,请发布所有代码。

答案 1 :(得分:2)

正如评论中所说,您在初始化之前使用的是txtPrice。 (与setTableModel相比,请参阅方法调用txtPrice = new JLabel("");的顺序....你看,它早先被调用了)