AutoCompleteDecorator is interrupting ItemListener

守給你的承諾、 提交于 2019-12-08 11:42:39

问题


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:


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


来源:https://stackoverflow.com/questions/27103957/autocompletedecorator-is-interrupting-itemlistener

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!