Skip to main content

ArrayIndexOutOfBoundsException when invoke removeComponent()

4 replies [Last post]
fangyuancoolhero
Offline
Joined: 2010-01-19
Points: 0

Currently we have a project based on lwuit, but I got a ArrayIndexOutOfBoundsException when invorking removeComponent() in a form and the exception happens randomly.
The exception like this:
java.lang.ArrayIndexOutOfBoundsException: 2 >= 2
at java.util.Vector.elementAt(Vector.java:427)
at com.sun.lwuit.Container.getComponentAt(Container.java:579)
at com.sun.lwuit.Container.doLayout(Container.java:551)
at com.sun.lwuit.Container.layoutContainer(Container.java:540)
at com.sun.lwuit.Container.paint(Container.java:495)
at com.sun.lwuit.Component.paintInternal(Component.java:814)
at com.sun.lwuit.Container.paint(Container.java:500)
at com.sun.lwuit.Form.paint(+10)
at com.sun.lwuit.Component$BGPainter.paint(Component.java:2432)
at com.sun.lwuit.Component.drawPainters(Component.java:1015)
at com.sun.lwuit.Component.drawPainters(Component.java:979)
at com.sun.lwuit.Component.paintBackgrounds(Component.java:742)
at com.sun.lwuit.Component.paintComponent(Component.java:950)
at com.sun.lwuit.Component.paintComponent(Component.java:907)
at com.sun.lwuit.impl.LWUITImplementation.paintDirty(LWUITImplementation.java:240)
at com.sun.lwuit.Display.edtLoopImpl(Display.java:618)
at com.sun.lwuit.Display.mainEDTLoop(Display.java:572)
at com.sun.lwuit.RunnableWrapper.run(RunnableWrapper.java:120)

Can anybody help to resolve this problem?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Arnau Vazquez

Are you calling removeComponent() from the EDT?

2010/1/19

> Currently we have a project based on lwuit, but I got a
> ArrayIndexOutOfBoundsException when invorking removeComponent() in a form
> and the exception happens randomly.
> The exception like this:
> java.lang.ArrayIndexOutOfBoundsException: 2 >= 2
> at java.util.Vector.elementAt(Vector.java:427)
> at com.sun.lwuit.Container.getComponentAt(Container.java:579)
> at com.sun.lwuit.Container.doLayout(Container.java:551)
> at com.sun.lwuit.Container.layoutContainer(Container.java:540)
> at com.sun.lwuit.Container.paint(Container.java:495)
> at com.sun.lwuit.Component.paintInternal(Component.java:814)
> at com.sun.lwuit.Container.paint(Container.java:500)
> at com.sun.lwuit.Form.paint(+10)
> at com.sun.lwuit.Component$BGPainter.paint(Component.java:2432)
> at com.sun.lwuit.Component.drawPainters(Component.java:1015)
> at com.sun.lwuit.Component.drawPainters(Component.java:979)
> at com.sun.lwuit.Component.paintBackgrounds(Component.java:742)
> at com.sun.lwuit.Component.paintComponent(Component.java:950)
> at com.sun.lwuit.Component.paintComponent(Component.java:907)
> at
> com.sun.lwuit.impl.LWUITImplementation.paintDirty(LWUITImplementation.java:240)
> at com.sun.lwuit.Display.edtLoopImpl(Display.java:618)
> at com.sun.lwuit.Display.mainEDTLoop(Display.java:572)
> at com.sun.lwuit.RunnableWrapper.run(RunnableWrapper.java:120)
>
>
> Can anybody help to resolve this problem?
> [Message sent by forum member 'fangyuancoolhero' (yuan@ncsi.com.cn)]
>
> http://forums.java.net/jive/thread.jspa?messageID=381668
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@lwuit.dev.java.net
> For additional commands, e-mail: users-help@lwuit.dev.java.net
>
>
[att1.html]

fangyuancoolhero
Offline
Joined: 2010-01-19
Points: 0

Thank you for your response.

I think I 'm calling removeComponent() in my own thread.

But I'm not sure about this because I'm not understanding EDT very well.

Arnau Vazquez

Hi Yuan,

If you remove a component from a thread other than the EDT, you will have a
race condition if the latter is doing some operation related with the
form/component, like painting it. Therefore you should do all graphical
operations (add/remove) in the EDT, to avoid this. You can use something
like this:

if (Display.getInstance().isEdt()) {
// remove component
} else {
Display.getInstance().callSeriallyAndWait(new Runnable() {
public void run() {
// remove component
}
});
}

If you know for sure you are not in the EDT (e.g. it's a thread you created
yourself) or that you indeed are (e.g. on action performed) then you can
skip the if/else and go straight to the right option.

Hope it helped,
Arnau

2010/1/20

> Thank you for your response.
>
> I think I 'm calling removeComponent() in my own thread.
>
> But I'm not sure about this because I'm not understanding EDT very well.
> [Message sent by forum member 'fangyuancoolhero' (yuan@ncsi.com.cn)]
>
> http://forums.java.net/jive/thread.jspa?messageID=381851
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@lwuit.dev.java.net
> For additional commands, e-mail: users-help@lwuit.dev.java.net
>
>
[att1.html]

fangyuancoolhero
Offline
Joined: 2010-01-19
Points: 0

Hi Arnau,

I have tried you suggestion and it seems to work correctly.Thank you very much.

Best Regards,

Yu An.