Question

I have a random appearing NullPointerException in a lil Application I wrote. Basically it shall display a list of numbers and when selecting one of them, some details shall be displayed in an other part of the window.

When Clicking on a Button to add new set of data, a new Window opens, with some textfields and an button to fire it to the database and update the list to include the net data set, as well as selecting that one.

At this position, I very randomly get an NPE (see log below). Fun thing is - everything workt all right, either when there is an error or not. Only the Window doesn't close then. When I run in debug mode it NEVER happens.

To see the Log: http://pastebin.com/FQzp6Wqp

This is the SelectionListener:

menu.itemList.addListSelectionListener(new ListSelectionListener() {
    @Override
    public void valueChanged(ListSelectionEvent e) {
        String index = menu.itemList.getSelectedValue().toString(); //mentioned Line 63
        TDMInvDB db = new TDMInvDB();
        try {
            details.descr.setText(db.getDescr(index));
            details.specs.setText(db.getSpecs(index));
            details.historyList.setListData(db.getHistory(index));
            if(Integer.parseInt(index) < 0) {
                details.add.setEnabled(false);
                details.edit.setEnabled(false);
            } else {
                details.add.setEnabled(true);
                details.edit.setEnabled(true);
            }
        } catch (ClassNotFoundException e1) {
            e1.printStackTrace();
        }
    }
});

This is the ActionListener of the Button to fire to the database:

add.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent ae) {
        TDMInvDB db = new TDMInvDB();
        try {
            db.addItem(inv.getText(), descr.getText(), specs.getText());
            m.itemList.setSelectedIndex(-1);
            m.itemList.setListData(db.getItemList());    //mentioned Line 108
            m.itemList.setSelectedValue(inv.getText(), true);
            ai.dispose();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
});
Was it helpful?

Solution

How about you insert

if (menu.itemList.getSelectedValue() == null) 
    return; 

before

String index = menu.itemList.getSelectedValue().toString();

If there is nothing selected then you don't want to do anything.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top