[java] Get combobox value in Java swing

I need to get the integer value of the combobox in Swing.

I have set an integer value as id for the combobox.I tried combobox.getSelectedItem() and combobox.getSelectedIndex() but it cant get the int value.

Below is my code:

CommonBean commonBean[]=new CommonBean[commonResponse.getCommonBean().length+1];         
         for(int i=0;i<commonResponse.getCommonBean().length;i++)
         {
             commonBean[i] = new CommonBean("--Please select a project--", 0);
             commonBean[i+1] = new CommonBean(commonResponse.getCommonBean()[i].getProjectName(), commonResponse.getCommonBean()[i].getProjectId());
         }

JComboBox combobox= new JComboBox(commonBean);


public CommonBean(String projectName,int projectId) {       
        this.projectName = projectName;
        this.projectId = projectId;

    }

Any help is appreciated.

This question is related to java swing

The answer is


If the string is empty, comboBox.getSelectedItem().toString() will give a NullPointerException. So better to typecast by (String).