Reputation: 81
can someone help me out with this problem. this is the error message I get:
Exception in thread "LWJGL Application" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(ArrayList.java:635)
at java.util.ArrayList.get(ArrayList.java:411)
at com.mygdx.Papermadness.Papermadness.render(Papermadness.java:102)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:206)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)
This is the code of my for loop through the huisArray (huis= house).
private ArrayList<Sprite> huisArray = new ArrayList<Sprite>();
spriteBatch.begin();
sprite.draw(spriteBatch);
for (int i = 0; i < huisArray.size(); i++) {
huisArray.get(i).setY(huisArray.get(i).getY() - huisVelocity * delta);
if (huisArray.get(i).getY() <= 200) {
huisArray.remove(huisArray.get(i));
}
}
if (huisArray.get(0).getY() < 1200) {
addNewHuis();
}
huisBatch.begin();
for (int i = 0; i < huisArray.size(); i++) {
huisArray.get(i).setY(huisArray.get(i).getY() - huisVelocity * delta);
if (huisArray.get(i).getY() <= 200) {
huisArray.remove(huisArray.get(i));
}
}
if (huisArray.get(0).getY() < 1200) {
addNewHuis();
}
huisBatch.begin();
for (int i = 0; i < huisArray.size(); i++) {
huisBatch.draw(huisArray.get(i), huisArray.get(i).getX(), huisArray.get(i).getY());
}
spriteBatch.end();
huisBatch.end();
The idea of this code is that houses at the two edges of the screen fall continuesly.
Any help would be appreciated :)
Upvotes: 0
Views: 1405
Reputation: 721
Here:
if (huisArray.get(0).getY() < 1200) {
addNewHuis();
}
You're accessing to the first element in the huisArray, but never checking if it's empty.
Add a size check before:
if (huisArray.size > 0 &&huisArray.get(0).getY() < 1200) {
addNewHuis();
}
There's also another problem in your for loops:
for (int i = 0; i < huisArray.size(); i++) {
huisArray.get(i).setY(huisArray.get(i).getY() - huisVelocity * delta);
if (huisArray.get(i).getY() <= 200) {
huisArray.remove(huisArray.get(i));
}
}
If you remove things inside the for loop just like that, you'll be ignoring some items:
Let's say you're array have 3 elements [A, B, C], and the first one have the conditions to be removed.
So in the first loop, you'll have:
i = 0 array = [A, B, C]
A is removed, and the second loop starts
i = 1 array = [B, C]
See that now you'll check for C (which is in position 1), and you'll never check B
To avoid this (using that kind of for loop) just add:
for (int i = 0; i < huisArray.size(); i++) {
huisArray.get(i).setY(huisArray.get(i).getY() - huisVelocity * delta);
if (huisArray.get(i).getY() <= 200) {
huisArray.remove(huisArray.get(i));
i--;
}
}
Also as an advice, if you know the position of the item you wan't to remove, remove it by index instead of removing by object (it's faster)
huisArray.remove(i);
Upvotes: 0
Reputation: 14408
The problem is that huisArray
is empty and you are getting values from this list at line
if (huisArray.get(0).getY() < 1200) {
So first add items in huisArray
before checking item in huisArray
as
private ArrayList<Sprite> huisArray = new ArrayList<Sprite>();
// Add huisArray item here as huisArray.add(addspritehere);
Or change
if (huisArray.get(0).getY() < 1200) {
to
if (huisArray.size() > 0 && huisArray.get(0).getY() < 1200) {
Upvotes: 1
Reputation: 3229
Your error is likely right here:
if (huisArray.get(0).getY() < 1200) {
If your huisArray
has a size of zero, then getting an object out of it will throw an exception. You should check if it has anything in it first, as follows
if (huisArray.size() > 0 && huisArray.get(0).getY() < 1200) {
Upvotes: 1