arraylist - java.lang.ArrayIndexOutOfBoundsException: 2 in a List<HashMap<String,String>> -


i have method must insert in arraylist elements in way:

0 low 10 0 high 11 0 high 3 1 low 54 

for this, use hashmap last 2 elements in row , put hashmap in arraylist. code:

private void jbutton2actionperformed(java.awt.event.actionevent evt) {                                              // todo add handling code here:     hashmap<string, string> map = new hashmap<>();     list<hashmap<string, string>> data = new arraylist<>();     string label[];     jcheckbox casella=new jcheckbox();      //system.out.println(data.get(0).get("name"));      if(jlist2.getmodel().getsize()>0){ //se sono state selezionate pad         for(int i=0; i<jpanel2.getcomponentcount(); i++){ //controlla se le pad hanno prode             label=(casella.gettext()).split(" ");             if( (casella=(jcheckbox) jpanel2.getcomponent(i)).isselected() ){ //si                 map.put(label[2], label[4]);                 data.add(1,map);             }             else{ //no                 map.put(label[2], label[4]);                 data.add(0,map);             }         }         system.out.println(data.size()); 

the trouble on lines map.put(label[2], label[4]); have message:

exception in thread "awt-eventqueue-0" java.lang.arrayindexoutofboundsexception: 2 

i can not understand wrong.

if string array resulting from

label=(casella.gettext()).split(" "); 

doesn't have enough elements (less 2 resp. 4), exception.

you should find out how long array before using it.


Comments