有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何解决此处的空指针异常错误?

public class paymentSystemGUI extends javax.swing.JFrame {
    private JScrollPane jScrollListInventory;
    private JScrollPane jScrollCart;
    private JList cartList;
    private DefaultListModel stock = new DefaultListModel();
    private JList inventList;
    private inventoryList stockInst;
    private inventItem invent1;
    private DefaultListModel checkoutBasket = new DefaultListModel();
    private JButton addBtn;
    private JLabel priceLbl;
    private JLabel idLabel;
    private JLabel nameLabel;
    private JTextField itemNameField;
    private JTextField itemIdField;
    private JTextField pricefld;


    /**
    * Auto-generated main method to display this JFrame
    */
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                paymentSystemGUI inst = new paymentSystemGUI();
                inst.setLocationRelativeTo(null);
                inst.setVisible(true);
            }
        });
    }

    public paymentSystemGUI() {
        super();
        initGUI();
    }


    private void initGUI() {
        try {
            BorderLayout thisLayout = new BorderLayout();
            setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
            getContentPane().setLayout(null);
            {
                jScrollListInventory = new JScrollPane();
                getContentPane().add(jScrollListInventory);
                jScrollListInventory.setBounds(47, 33, 347, 304);
                {
                    ListModel inventListModel = 
                            new DefaultComboBoxModel(
                                    new String[] { "Item One", "Item Two" });
                    inventList = new JList();
                    jScrollListInventory.setViewportView(inventList);
                    inventList.setModel(stock);
                    inventList.setBounds(267, 33, 352, 314);
                }
            }
            {
                jScrollCart = new JScrollPane();
                getContentPane().add(jScrollCart);
                jScrollCart.setBounds(439, 44, 280, 293);
                {
                    ListModel cartListModel = 
                            new DefaultComboBoxModel(
                                    new String[] { "Item One", "Item Two" });
                    cartList = new JList();
                    jScrollCart.setViewportView(cartList);
                    cartList.setModel(checkoutBasket);
                    cartList.setBounds(454, 58, 296, 340);
                }
            }
            {
                itemNameField = new JTextField();
                getContentPane().add(itemNameField);
                itemNameField.setBounds(34, 359, 113, 23);
            }
            {
                itemIdField = new JTextField();
                getContentPane().add(itemIdField);
                itemIdField.setBounds(159, 359, 105, 23);
            }
            {
                nameLabel = new JLabel();
                getContentPane().add(nameLabel);
                nameLabel.setText("Name of item");
                nameLabel.setBounds(32, 394, 115, 16);
            }
            {
                idLabel = new JLabel();
                getContentPane().add(idLabel);
                idLabel.setText("Id number");
                idLabel.setBounds(164, 394, 105, 16);
            }
            {
                priceLbl = new JLabel();
                getContentPane().add(priceLbl);
                priceLbl.setText("Price");
                priceLbl.setBounds(297, 394, 76, 16);
            }
            {
                addBtn = new JButton();
                getContentPane().add(addBtn);
                addBtn.setText("Add Item to Stock");
                addBtn.setBounds(38, 432, 109, 23);
                addBtn.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent addItem) {
                        addButtonPressed();

                    }
                });
            }
            {
                pricefld = new JTextField();
                getContentPane().add(pricefld);
                pricefld.setBounds(286, 359, 94, 23);
            }
            pack();
            this.setSize(788, 521);
        } catch (Exception e) {
            //add your error handling code here
            e.printStackTrace();
        }
    }

    private void addButtonPressed() {
        String newid = itemIdField.getText();
        String newitemName = itemNameField.getText();
        String newprice = pricefld.getText();

        if (newid.equals("") || newitemName.equals("") || newprice.equals("")) {
            JOptionPane.showMessageDialog(this, "Please Enter Full Details");
        } else {
            stockInst.addInventItem(newid, newitemName, newprice);
            inventItem newBasket = stockInst.findItemByName(newid);
            inventList.setSelectedValue(newBasket, true);
            clearAllTextFields();
        }
    }

    private void clearAllTextFields() {
        itemIdField.setText("");
        itemNameField.setText("");
        pricefld.setText("");
    }

}

大家好。例外情况是链接到我的GUI中的一个按钮的addButtonPressed方法。我正在尝试向我的应用程序中的jlist添加一组详细信息。指向第164行的点:

stockInst.addInventItem(newid, newitemName, newprice);

谢谢你的帮助


共 (1) 个答案

  1. # 1 楼答案

    在使用前,必须将inventoryList的实例分配给stockInst

            stockInst = new inventoryList ();
            ...
            stockInst.addInventItem(newid, newitemName, newprice);
            inventItem newBasket = stockInst.findItemByName(newid);