jComboBox+java.lang.StringIndexOutOfBoundsException

In theuser interface, I am selecting the values from4 jComboBox's and concatenating them in theString total. Thetotal is getting printed if I select all the4 jComboBox's.

But sometimes I need to select the values of

1. onejComboBox only

2. twojComboBox only

3. threejComboBox only

But if I don't select alljComboBox's, it's throwing me an error as

Exception in thread "AWT-EventQueue-0" java.lang.StringIndexOutOfBoundsException: String index out of range: 1

My code is:

jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[]{"","a","b","c","d","h"}));

String jComboBox1Text=jComboBox1.getSelectedItem().toString();

String jComboBox2Text=jComboBox2.getSelectedItem().toString();

String jComboBox3Text=jComboBox3.getSelectedItem().toString();

String jComboBox4Text=jComboBox4.getSelectedItem().toString();

String total=jComboBox1Text+jComboBox2Text+jComboBox3Text+jComboBox4Text;

[1338 byte] By [Simmya] at [2007-11-26 14:54:28]
# 1
In future Swing related questions should be posted into the Swing forum.Perhaps you should be looking to see if ANY item is selected?
cotton.ma at 2007-7-8 8:42:55 > top of Java-index,Java Essentials,New To Java...
# 2
Perhaps you should be looking to see if ANY item is selected? Can you please tell me what do you mean by this?
Simmya at 2007-7-8 8:42:55 > top of Java-index,Java Essentials,New To Java...
# 3
> Perhaps you should be looking to see if ANY item> is selected? > > Can you please tell me what do you mean by this?If a JComboBox has no selected item then selecting the item and calling toString on it seems pretty futile doesn't it?
cotton.ma at 2007-7-8 8:42:55 > top of Java-index,Java Essentials,New To Java...
# 4
Exception in thread "AWT-EventQueue-0" java.lang.StringIndexOutOfBoundsException: String index out of range: 1This suggests you are trying to access the 2nd char of a string that is less than 2 chars long. Nothing to do with the combobox.
floundera at 2007-7-8 8:42:55 > top of Java-index,Java Essentials,New To Java...
# 5
Thanks for the solution.
Simmya at 2007-7-8 8:42:55 > top of Java-index,Java Essentials,New To Java...