Confusion With ArrayLists

Here’s a basic version of my Class “Power System”, I have cut most of the irrelevant code out.
Here I am adding Integers to an ArrayList, this works fine however when attempting to remove an object from the list
I get an error: “java.lang.IndexOutOfBoundsException: Index: 0, Size: 0” I am confused because this error is saying that my list contains nothing at index: 0 even though there is definitely an object or two in the list. I think this is all of the relevant code.

public ArrayList powers;

public void addPower(int id){
if(powers.size() < 3){
powers.add(id);
}

		for(int i : powers){
			System.out.println(i);
		}
}

public void removePower(){
				powers.remove(0);
	
}

public void useAbility(){
		removePower();
}


public void tick(){ // I know I should be using an array or something here

	if(!powers.isEmpty()){
			currentPower = powers.get(0);
		if(powers.size() == 2)
			secondPower = powers.get(1);
		if(powers.size() == 3)
			thirdPower = powers.get(2);
	}
}

Do a check

if (!powers.isEmpty()) {
powers.remove(0);
}

adding this prevents my game from crashing which made me realize that my code was indeed working. I now realize that the problem I’m having isn’t with the ArrayList so thank-you. When removing the index I want an image to stop rendering on the screen but clearly I’m not going about it correctly:

here’s some more of my code, “10” is the value of the integer I am adding to my ArrayList

public void render(Graphics g){
	switch(currentPower){
	case 10: g.drawImage(Assets.invulnPowerIcon, 250, 12, null);
	}
	
	switch(secondPower){
	case 10: g.drawImage(Assets.invulnPowerIcon, 300, 12, null);
	}
	
	switch(thirdPower){
	case 10: g.drawImage(Assets.invulnPowerIcon, 350, 12, null);
	}
	
}

EDIT:

fixed it with

public void tick(){
	

	if(!powers.isEmpty())
		for(int i = 0; i < powers.size(); i++){
			currentPowers[i] = powers.get(i);
		}
		
	
	for(int i : powers){
		System.out.println(i);
	}
}

public void render(Graphics g){
	if(!powers.isEmpty())
		if(currentPowers[0] == 10){
			g.drawImage(Assets.invulnPowerIcon, 15, 250, null);
		}
	
}

Just a small tip —

Create a Power class and give it an id field.

Should use a Power class and have an ArrayList of these :slight_smile: