"java.lang.ArrayIndexOutOfBoundsException: -1" while adding tab to "javax.swing.JTabbedPane" -


please resolved java.lang.arrayindexoutofboundsexception.

i getting exception quite randomly , worst part not reproduce exception. stack-trace shows addtab() thrown exception.

here code-

public class closabletabbedpane extends jtabbedpane{      public void addtab(string title, component component) {             super.addtab(title+"       ", component);         }       public string gettabtitleat(int index) {         return super.gettitleat(index).trim();     }      public void removetab(int index){             this.removetabat(index);         } 

here stack trace-

21 may 2013 09:38:11,992  error eerror : java.lang.arrayindexoutofboundsexception: -1        @ java.util.arraylist.elementdata(arraylist.java:371)        @ java.util.arraylist.get(arraylist.java:384)        @ javax.swing.jtabbedpane.gettitleat(jtabbedpane.java:1112)  @ javax.swing.plaf.basic.basictabbedpaneui$handler.updatehtmlviews(basictabbedpaneui.java:3578)        @ javax.swing.plaf.basic.basictabbedpaneui$handler.componentadded(basictabbedpaneui.java:3713)    @ java.awt.container.processcontainerevent(container.java:2255)        @ java.awt.container.processevent(container.java:2226)        @ java.awt.component.dispatcheventimpl(component.java:4861)   @ java.awt.container.dispatcheventimpl(container.java:2287)        @ java.awt.component.dispatchevent(component.java:4687)        @ java.awt.container.addimpl(container.java:1131)        @ javax.swing.jtabbedpane.inserttab(jtabbedpane.java:724)     @ javax.swing.jtabbedpane.addtab(jtabbedpane.java:798)        @ net.abc.f.c.b.addtab(unknown source)        @ net.abc.f.a.c.a(unknown source)        @ com.xyz.b.b.f.<init>(unknown source)        @ com.xyz.b.b.f.<init>(unknown source)        @ com.xyz.main.c.w.a(unknown source)         @ com.xyz.main.c.o.run(unknown source)        @ java.lang.thread.run(thread.java:722) -1 error: java.lang.arrayindexoutofboundsexception: -1 

because somehow passing index -1 element arraylist, , index -1 invalid index. index in arraylist 0 based , get(index) throw indexoutofboundexception if there no element stored @ passed index.

you need make check valid index before calling get(index) method of arraylist. like,

public string gettitleat(index) {   if(index >=0 && index<list.size()) {        list.get(index);   }   return null; //fallback } 

Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -