java - 这里如何解决NullPointer异常错误?

标签 java swing

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("");
    }

}

大家好。异常(exception)情况是链接到 GUI 中按钮之一的 addButtonPressed 方法。我正在尝试将一组详细信息添加到我的应用程序中的 jlist 中。指向第 164 行:

stockInst.addInventItem(newid, newitemName, newprice);

感谢您的帮助。

最佳答案

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

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

关于java - 这里如何解决NullPointer异常错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29259306/

相关文章:

java - Spring 安全 : Authentication manager and global security config with Java config from xml config

java - 为什么设置 JDialog 或 JFrame setVisible(true) 会切换我的 IME 设置?

Java - 重新绘制特定组件

java - 使用 Swing Timer 延迟任务

java - 如何监听鼠标滚轮按下?

java - 尝试使用 Java 对新实例进行分类并定义了 Weka-No 输出实例格式时出错

java - For 循环使用参数 start 和 end 搜索字符的出现

java - Spring Security 做自动过滤注入(inject)吗?

java - 使用 JAX-WS 尝试所有可用的 WSDL IP

java - GUI 创建时程序不退出