AutoCompleteDecorator is interrupting ItemListener

136 views Asked by At

I have an editable JComboBox which is integrated with the AutoCompleteDecorator of SwingX library. My JComboBox is also having an ItemListener registered to it as well. Now, Please have a look at the below code.

AutoCompleteDecorator.decorate(ClientNameCombo);
ClientNameCombo.addItemListener(new ClientNameComboAction());

private class ClientNameComboAction implements ItemListener
     {

        @Override
        public void itemStateChanged(ItemEvent e) 
        {
            String selectedClientName= ClientNameCombo.getSelectedItem().toString();


            if(!selectedClientName.equals("Select Client"))
            {
                int idClient = Integer.parseInt(String.valueOf(client_name_id_map.get(selectedClientName)));

                String sql = "r";


            }
        }
     }

No matter what, my code do not pass int idClient = Integer.parseInt(String.valueOf(client_name_id_map.get(selectedClientName))); it always ended up with NumberFormatException. The amazing thing is, if I remove AutoCompleteDecorator then everything works fine.

Anyone know how to fix this please?

1

There are 1 answers

0
Eran On

The problem would occur when the key you are looking for is not found in the map.

In such case :

  • client_name_id_map.get(selectedClientName) would return null
  • String.valueOf(client_name_id_map.get(selectedClientName)) would return "null"
  • and Integer.parseInt("null") would throw an exception

A simple solution :

        if(!selectedClientName.equals("Select Client"))
        {
            Integer idClient = client_name_id_map.get(selectedClientName);
            if (idClient != null) {
                // do something
            }

            String sql = "r";
        }