gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 04:50:47 25 4
gpt4 key购买 nike

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com