ArrayList.throwIndexOutOfBoundsException error

  ... discussions about development with the GLES2 branch of AndEngine.

ArrayList.throwIndexOutOfBoundsException error

Postby Rafael » Thu Nov 22, 2012 3:20 pm

Hi All.

I get an Error like this when I press the "back" button in my game.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. 11-22 14:09:49.029: W/EglHelper(32384): destroySurface()  tid=13
  2. 11-22 14:09:49.039: W/EglHelper(32384): finish() tid=13
  3. 11-22 14:09:49.049: W/dalvikvm(32384): threadid=10: thread exiting with uncaught exception (group=0x40018578)
  4. 11-22 14:09:49.049: E/AndroidRuntime(32384): FATAL EXCEPTION: GLThread 13
  5. 11-22 14:09:49.049: E/AndroidRuntime(32384): java.lang.IndexOutOfBoundsException: Invalid index 161, size is 161
  6. 11-22 14:09:49.049: E/AndroidRuntime(32384):    at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257)
  7. 11-22 14:09:49.049: E/AndroidRuntime(32384):    at java.util.ArrayList.get(ArrayList.java:311)
  8. 11-22 14:09:49.049: E/AndroidRuntime(32384):    at org.andengine.entity.Entity.onManagedDraw(Entity.java:1382)
  9. 11-22 14:09:49.049: E/AndroidRuntime(32384):    at org.andengine.entity.scene.Scene.onManagedDraw(Scene.java:260)
  10. 11-22 14:09:49.049: E/AndroidRuntime(32384):    at org.andengine.entity.Entity.onDraw(Entity.java:1160)
  11. 11-22 14:09:49.049: E/AndroidRuntime(32384):    at org.andengine.engine.Engine.onDrawScene(Engine.java:627)
  12. 11-22 14:09:49.049: E/AndroidRuntime(32384):    at org.andengine.engine.Engine.onDrawFrame(Engine.java:617)
  13. 11-22 14:09:49.049: E/AndroidRuntime(32384):    at org.andengine.opengl.view.EngineRenderer.onDrawFrame(EngineRenderer.java:105)
  14. 11-22 14:09:49.049: E/AndroidRuntime(32384):    at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1388)
  15. 11-22 14:09:49.049: E/AndroidRuntime(32384):    at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1138)
  16. 11-22 14:09:49.059: D/AndEngine(32384): BaseActivity.onPause @(Thread: 'main')
  17. 11-22 14:09:49.059: I/GLThread(32384): onPause tid=13
  18. 11-22 14:09:49.059: D/AndEngine(32384): BaseActivity.onPauseGame @(Thread: 'main')
  19. 11-22 14:09:49.059: E/SensorManager(32384): unregisterListener:: handle = 0 Listener= org.andengine.engine.Engine@40570e50 name = BMA220
  20.  
Parsed in 0.015 seconds, using GeSHi 1.0.8.4


The Code for the "back" button looks like this.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. startCollision = false;
  2. this.getEngine().unregisterUpdateHandler(changeTimerHandler);
  3. this.getEngine().unregisterUpdateHandler(speedFastTimerHandler);
  4. this.getEngine().unregisterUpdateHandler(speedSlowTimerHandler);
  5. this.getEngine().unregisterUpdateHandler(spriteTimerHandler);
  6. this.getEngine().unregisterUpdateHandler(tickTimerHandler);
  7. if (player != null && player.getUserData() != null)
  8.     if (player.getUserData().equals("Alive"))
  9.     {
  10.         player.setUserData("Delete");
  11.         mPhysicsWorld.unregisterPhysicsConnector(
  12.             mPhysicsWorld.getPhysicsConnectorManager().findPhysicsConnectorByShape(player));
  13.         mPhysicsWorld.destroyBody(Playerbody);
  14.         player.clearUpdateHandlers();
  15.         mCurrentScene.detachChild(player);
  16.                                                
  17.     }
  18.     GPresent = new ArrayList<Sprite>();
  19.     BPresent = new ArrayList<Sprite>();
  20.     YPresent = new ArrayList<Sprite>();
  21.     RPresent = new ArrayList<Sprite>();
  22.     GBomb = new ArrayList<Sprite>();
  23.     BBomb = new ArrayList<Sprite>();
  24.     YBomb = new ArrayList<Sprite>();
  25.     RBomb= new ArrayList<Sprite>();
  26.     Walls= new ArrayList<Shape>();
  27.                                
  28.     moving = 0;
  29.     spawned = 0;
  30.     speed = 5f;
  31.     TimeLeft = 40;
  32.     allowCheck = 0;
  33.     points = 0;
  34.     lives = 3;
  35.     life = 3;
  36.     i = 0;
  37.     this.setCurrentScene(new PickRoundScene());
  38.  
Parsed in 0.012 seconds, using GeSHi 1.0.8.4


I have no idea what makes it crash. Can any1 help?
If U need more code then PM me plz.
Rafael
 
Posts: 61
Joined: Wed Sep 05, 2012 1:57 pm

Re: ArrayList.throwIndexOutOfBoundsException error

Postby alenko » Thu Nov 22, 2012 8:38 pm

instead of


mCurrentScene.detachChild(player);

use

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. final EngineLock engineLock = youractivity.this.mEngine.getEngineLock();
  2.                                                         engineLock.lock();
  3.  
  4.                                                         /* Now it is save to remove the entity! */
  5.                                                          mCurrentScene.detachChild(player);
  6.                                                         engineLock.unlock();
Parsed in 0.010 seconds, using GeSHi 1.0.8.4




i think :) try it
alenko
 
Posts: 45
Joined: Sun Aug 05, 2012 11:48 am

Re: ArrayList.throwIndexOutOfBoundsException error

Postby thepi » Fri Nov 23, 2012 12:45 am

How and where do you define Playerbody?

This forum is supposed to help anyone who might have similar issues, to find the solution, so I don't think PM is the right place to post the relevant code.
Go and trap them... Trap Balls on Google Play
Play & reminisce... Treasure Island LCD Retro
thepi
 
Posts: 456
Joined: Sun Oct 09, 2011 9:30 pm

Re: ArrayList.throwIndexOutOfBoundsException error

Postby Rafael » Fri Nov 23, 2012 4:16 pm

I already solved the problem.
I add/delete sprites and bodies and manage scenes in:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. runOnUpdateThread(new Runnable() {
  2.             @Override
  3.              public void run() { }
  4. });
  5.  
Parsed in 0.010 seconds, using GeSHi 1.0.8.4


Found some similar issues in google and many say to manage stuff in runOnUpdateThread - worked for me and I'm happy.

Maybe not long I'll and a topic with some SS from my game and when it's done I'll add I link to download it too.
Thx every1, have a nice day. :)
Rafael
 
Posts: 61
Joined: Wed Sep 05, 2012 1:57 pm

Re: ArrayList.throwIndexOutOfBoundsException error

Postby alenko » Fri Nov 23, 2012 6:46 pm

well the code i posted is kinda new way and unpdatethread old way, i think.
alenko
 
Posts: 45
Joined: Sun Aug 05, 2012 11:48 am

Re: ArrayList.throwIndexOutOfBoundsException error

Postby OzLark » Sat Nov 24, 2012 12:12 am

alenko wrote:well the code i posted is kinda new way and unpdatethread old way, i think.


Are you sure?

Using the lock method you risk dead lock and should only every be needed from a thread other than the update thread.

The runOnUpdateThread() is a bit of a misnomer as it is actually usually invoked from the update thread, but will be run at a safe time on the update thread..
Did this post help you? Feel free to return the gesture by downloading Ninja Bees, creating a level (or more) and sharing it to the Ninja Bees website!

Ninja Bees - Our latest Android game, check it out on Google Play (free) or paid with no ads
OzLark
 
Posts: 1130
Joined: Sun Feb 05, 2012 3:19 am

Re: ArrayList.throwIndexOutOfBoundsException error

Postby Rafael » Wed Nov 28, 2012 11:50 am

When I tried the lock and unlock method I got an ArrayList.throwIndexOutOfBoundsException error, so I'm staying with UpdateThread.

But now I have a different problem, when I do the reset and scene change in the UpdateThread I get "Fatal Signal 11" or something like this...

I tried redoing the Reset and Detach methods but I always get this error "fatal Signal 11".
It doesnt happen always. I play my round, lose it, press try again, play, lose, try again, play, lose, try again and it happens mostly after the third lose (I checked it a sec ago and +50% of the fatal signals are at the third reset the rest are random).

I have my code on github, so if some1 wants to help I can send a link to the source.
Rafael
 
Posts: 61
Joined: Wed Sep 05, 2012 1:57 pm

Re: ArrayList.throwIndexOutOfBoundsException error

Postby OzLark » Wed Nov 28, 2012 12:42 pm

Rafael wrote:...I get "Fatal Signal 11"


That will be box2d and not the engine - look at what you're doing with bodies, physics connectors and the physical world...
Did this post help you? Feel free to return the gesture by downloading Ninja Bees, creating a level (or more) and sharing it to the Ninja Bees website!

Ninja Bees - Our latest Android game, check it out on Google Play (free) or paid with no ads
OzLark
 
Posts: 1130
Joined: Sun Feb 05, 2012 3:19 am

Re: ArrayList.throwIndexOutOfBoundsException error

Postby Rafael » Wed Nov 28, 2012 12:54 pm

This is what I do:

Reset:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public void Reset(final String GoToScene)
  2.  {
  3.         runOnUpdateThread(new Runnable()
  4.         {
  5.                 @Override
  6.             public void run()
  7.                 {
  8.                         myGarbageCollection();
  9.                         getEngine().clearUpdateHandlers();
  10.  
  11.                         GPresent.clear();
  12.                         BPresent.clear();
  13.                         YPresent.clear();
  14.                         RPresent.clear();
  15.                         GBomb.clear();
  16.                         BBomb.clear();
  17.                         YBomb.clear();
  18.                         RBomb.clear();
  19.                         Walls.clear();
  20.  
  21.                         ObjWallList = new Shape[1500][2];
  22.                         WallListInts = new float[1500][2];
  23.                        
  24.                         startCollision = false;
  25.                         moving = 0;
  26.                         spawned = 0;
  27.                         speed = 5f;
  28.                         TimeLeft = 40;
  29.                         allowCheck = 0;
  30.                         lives = 3;
  31.                         i = 0;
  32.                         timeout = 0;
  33.                        
  34.                         if (GoToScene.equals("win"))
  35.                         {
  36.                                 setCurrentScene(new WinScene());
  37.                         }
  38.                         else if (GoToScene.equals("time"))
  39.                         {
  40.                                 setCurrentScene(new TimeScene());
  41.                         }
  42.                         else if (GoToScene.equals("lose"))
  43.                         {
  44.                                 setCurrentScene(new LoseScene());
  45.                         }
  46.                 }
  47.         });
  48.     }
  49.  
Parsed in 0.012 seconds, using GeSHi 1.0.8.4


myGarbageCollection:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. private void myGarbageCollection()
  2.         {  
  3.         Iterator<Body> allMyBodies = this.mPhysicsWorld.getBodies();
  4.         while(allMyBodies.hasNext())
  5.         {
  6.              try {
  7.                  final Body myCurrentBody = allMyBodies.next();
  8.                  this.runOnUpdateThread(new Runnable(){
  9.                      @Override
  10.                      public void run() {
  11.                          mPhysicsWorld.destroyBody(myCurrentBody);                
  12.                      }
  13.                  });
  14.             } catch (Exception e) {
  15.                 Debug.d("SPK - THE BODY DOES NOT WANT TO DIE: " + e);
  16.             }
  17.         }
  18.  
  19.         Iterator<Joint> allMyJoints = this.mPhysicsWorld.getJoints();
  20.         while(allMyJoints.hasNext())
  21.         {
  22.              try {
  23.                  final Joint myCurrentJoint = allMyJoints.next();
  24.                  this.runOnUpdateThread(new Runnable(){
  25.                      @Override
  26.                      public void run() {
  27.                          mPhysicsWorld.destroyJoint(myCurrentJoint);                
  28.                      }
  29.                  });
  30.             } catch (Exception e) {
  31.                 Debug.d("SPK - THE JOINT DOES NOT WANT TO DIE: " + e);
  32.             }
  33.         }
  34.  
  35.         if(AllSprites.size()>0){
  36.                  this.runOnUpdateThread(new Runnable(){
  37.                      @Override
  38.                      public void run() {
  39.                        for(int i=0; i < AllSprites.size(); i++){                  
  40.                              try {
  41.                                  final int myI = i;
  42.                                          mCurrentScene.detachChild(AllSprites.get(myI));                
  43.                             } catch (Exception e) {
  44.                                 Debug.d("SPK - THE SPRITE DOES NOT WANT TO DIE: " + e);
  45.                             }
  46.                        }
  47.                       }
  48.                 });
  49.         }
  50.         AllSprites.clear();
  51.         mCurrentScene.back();
  52.         System.gc();
  53.         }      
  54.  
Parsed in 0.013 seconds, using GeSHi 1.0.8.4


This is how I call it:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. if (lives <= 0)
  2. {
  3.     Reset("lose");
  4. }
  5.  
Parsed in 0.010 seconds, using GeSHi 1.0.8.4


Am I missing something?
Rafael
 
Posts: 61
Joined: Wed Sep 05, 2012 1:57 pm

Re: ArrayList.throwIndexOutOfBoundsException error

Postby skunktrader » Wed Nov 28, 2012 1:04 pm

Joints are automatically deleted when the attached body is destroyed. Either delete your joints first or only delete the bodies
skunktrader
 
Posts: 93
Joined: Tue Jul 10, 2012 8:04 am
Location: Brisbane, Australia

Next

Return to GLES2

Who is online

Users browsing this forum: Exabot [Bot] and 51 guests