I am working on a minecraft mod. The id system for EntityC4 is not working. The code is
public static List C4 = new ArrayList();
public static EntityC4 getitemfromnumber(int num)
{
EntityC4 entity = (EntityC4)C4.get(num);
return entity;
}
public static void createdetonater(EntityC4 c4, int num)
{
C4.add(num, c4);
}
public static int getnum(){
int num = 0;
for(boolean a = true; a != false;){
EntityC4 c = (EntityC4)C4.get(num);
System.out.print("current num : " + num);
if(c != null)
{
a = false;
}else{
System.out.print("entity " + num + " is null" );
num++;
}
}
return num;
}
And when I use getnum() an error shows up that says
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.RangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
EDIT:Ty and the loop was supposed the first non-existent space in the list.
You create a new
ArrayList C4, you never put anything in theList, and then you ask it for the element at index0. This element does not exists since the List is still empty, and it throws an exception.You can avoid this exception by adjusting your method
This will avoid the exception, but you will get stuck in your loop since you never find the element (which isn’t available in the list)