If the list is reddots :

 ArrayList<Red> reddots = new ArrayList(); 

Items are added to it:

 class DotListener extends InputListener { @Override public boolean touchDown(InputEvent event, float x, float y, int pointer, int button){ event.getListenerActor().remove(); float xx = event.getListenerActor().getX(); float yy = event.getListenerActor().getY(); int ind = ((int)xx - 46) / 69; if (count % 2 == 0) { Red r = new Red(); r.setSize(30, 30); r.setPosition(xx, yy); r.setColor(Color.RED); reddots.add(ind, r); stage.addActor(r); } else { Blue b = new Blue(); b.setSize(30, 30); b.setPosition(xx, yy); b.setColor(Color.BLUE); stage.addActor(b); } count++; return true; } } 

Why does the code crash? If you remove reddots.add(ind, r) , it will not crash. But I need to give an index to each point.

Classes Red and Blue are inherited from Dot .

Dot class:

 class Dot extends Actor { //invisible @Override public void draw(Batch batch, float parentAlpha) { Color batchColor = batch.getColor(); final float r = batchColor.r; final float g = batchColor.g; final float b = batchColor.b; final float a = batchColor.a; Color dotColor = getColor(); batch.setColor(dotColor.r, dotColor.g, dotColor.b, dotColor.a * parentAlpha); batch.draw(dotimg, getX(), getY(), getWidth(), getHeight()); batch.setColor(r, g, b, a); } } 

Console Error:

E / AndroidRuntime: FATAL EXCEPTION: GLThread 151 Process: com.mygdx.game, PID: 2941 java.lang.IndexOutOfBoundsException: Index: 9, Size: 0 at java.util.ArrayList.add (ArrayList.java penet57) at com .mygdx.game.MyGdxGame $ DotListener. .notify (Actor.java:182) at com.badlogic.gdx.scenes.scene2d.Actor.fire (Actor.java:147) at com.badlogic.gdx.scenes.scene2d.Stage.touchDown (Stage.java around82 ) at com.badlogic.gdx.backends.android.AndroidInput.processEvents (AndroidInput.java天75) at com.badlogic.gdx.backends.android.AndroidGraphics.onDrawFrame (AndroidGraphics.java 27) at android.opengl.GLSurfaceView GLThread.guardedRun (GLSurfaceView.java:1548) at android.opengl.GLSurfaceView $ GLThread.run (GLSurfaceView.java:1259) E / AndroidGraphics: waiting for pause synchronization; assuming deadlock and killing

  • Isn't reddots randomly null ? - VladD
  • @VladD in the presented code, it is generally declared outside the class boundaries. One can only hope that this class DotListener is an internal class. - Regent
  • one
    To understand why the code crashes, it is worthwhile to first analyze the stacktrace of the exception. Add it to the question. - Regent
  • DotListener - inner class, reddots - main field - ramazan793
  • @Regent what is StackTrace? - ramazan793

1 answer 1

You create an empty list:

 ArrayList<Red> reddots = new ArrayList(); 

The default size ( size ) is 0 .

When you try to add an item to the list at a certain position, this check occurs:

 if (index > size || index < 0) throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); 

Since the index in your case is 9 (judging by the stacktrace), and the size is 0 , an exception is eventually thrown.

There are at least two solutions to the problem in this situation:

  1. Add items not to a specific index, but to the end of the list:

     reddots.add(r); 
  2. If you still need to add to a specific index, then you should take care of the size of the list when creating it:

     int dotsCount = 1000; Red[] dummy = new Red[dotsCount]; ArrayList<Red> reddots = new ArrayList<>(Arrays.asList(dummy)); 

Just in case, I will clarify why to fence such a code:

  • If you simply create an ArrayList with its initialCapacity , then its size will still be 0 and an IndexOutOfBoundsException will reappear.
  • If to make List<Red> reddots = Arrays.asList(dummy); then when you try to add an item to the list, UnsupportedOperationException will be thrown.