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

}

Hello all. The exception is on the addButtonPressed method linked to one of the buttons in my GUI. I am trying to add a set of details to a jlist within my app. Points to line 164 :

stockInst.addInventItem(newid, newitemName, newprice);

Thank you for any help.

  • 2
    You really don't want to use use `null` layouts and `setBounds`, not just because it proclaims your GUI as that of a newbie who doesn't know better, but more because you will have an extremely difficult time upgrading and enhancing the GUI should the need arise. For instance, it is so much easier adding one more JRadioButton to a GUI made with nested JPanels and layout managers than it is for one that uses null layouts. Also your GUI will look terrible on all platforms other than your own. Just don't do it. – Hovercraft Full Of Eels Mar 25 '15 at 15:08

1 Answers1

2

You have to assign an instance of inventoryList to stockInst before using it :

        stockInst = new inventoryList ();
        ...
        stockInst.addInventItem(newid, newitemName, newprice);
        inventItem newBasket = stockInst.findItemByName(newid);
Eran
  • 387,369
  • 54
  • 702
  • 768
  • 2
    1+, And fix your class names. Class names should start with an upper case character. "inventoryList" should be `InventoryList`. If you want people to read your code and answer questions then follow Java conventions, don't make up your own! Notice how other classnames are highlighted in the forum. Your custom classes are NOT highlighted and it makes it very confusing to read your code. – camickr Mar 25 '15 at 15:08