1

Please help to resolved java.lang.ArrayIndexOutOfBoundsException.

I am getting this exception quite randomly and the worst part is that I could not reproduce exception. Stack-Trace shows addTab() thrown that exception.

Here is the 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 is the stack trace-

21 May 2013 09:38:11,992  ERROR eError : java.lang.ArrayIndexOutOfBoundsException: -1
       at java.util.ArrayList.elementData(ArrayList.java:371)
       at java.util.ArrayList.get(ArrayList.java:384)
       at javax.swing.JTabbedPane.getTitleAt(JTabbedPane.java:1112)

at javax.swing.plaf.basic.BasicTabbedPaneUI$Handler.updateHtmlViews(BasicTabbedPaneUI.java:3578)
       at javax.swing.plaf.basic.BasicTabbedPaneUI$Handler.componentAdded(BasicTabbedPaneUI.java:3713)
   at java.awt.Container.processContainerEvent(Container.java:2255)
       at java.awt.Container.processEvent(Container.java:2226)
       at java.awt.Component.dispatchEventImpl(Component.java:4861)

 at java.awt.Container.dispatchEventImpl(Container.java:2287)
       at java.awt.Component.dispatchEvent(Component.java:4687)
       at java.awt.Container.addImpl(Container.java:1131)
       at javax.swing.JTabbedPane.insertTab(JTabbedPane.java:724)

   at javax.swing.JTabbedPane.addTab(JTabbedPane.java:798)
       at net.abc.f.c.b.addTab(Unknown Source)
       at net.abc.f.a.c.a(Unknown Source)
       at com.xyz.b.b.f.<init>(Unknown Source)
       at com.xyz.b.b.f.<init>(Unknown Source)
       at com.xyz.main.c.w.a(Unknown Source)
        at com.xyz.main.c.o.run(Unknown Source)
       at java.lang.Thread.run(Thread.java:722)
-1 Error: java.lang.ArrayIndexOutOfBoundsException: -1
Ashish Pancholi
  • 4,569
  • 13
  • 50
  • 88

1 Answers1

2

Because you are somehow passing index -1 to get the element from ArrayList, and index -1 is invalid index. Index in ArrayList are 0 based and get(index) will throw the IndexOutOfBoundException if there is no element stored at the passed index.

You need to make a check for valid index before calling get(index) method of ArrayList. Something like,

public String getTitleAt(index) {
  if(index >=0 && index<list.size()) {  
     list.get(index);
  }
  return null; //fallback
}
sanbhat
  • 17,522
  • 6
  • 48
  • 64
  • How you sure that this issue is not related with `Concurrency in Swing`? http://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html – Ashish Pancholi May 21 '13 at 06:02
  • is this issue is similar with http://stackoverflow.com/questions/11579556/swing-jtabbedpane-throws-indexoutofboundsexception-while-filling-up?rq=1 ? – Ashish Pancholi May 21 '13 at 06:03
  • I couldn't notice that the part of code which was throwing this error was Swing component. I just suggested general way of avoiding the exception. – sanbhat May 21 '13 at 06:24