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

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。有人知道哪里出了问题吗?

最佳答案

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

10-06 07:15