简体   繁体   English

索引越界异常

[英]Index Out Of Bounds Exception

I am working on a minecraft mod. 我正在开发《我的世界》 mod。 The id system for EntityC4 is not working. EntityC4的id系统无法正常工作。 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 当我使用getnum()时,出现错误提示

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. 编辑:Ty,循环被认为是列表中的第一个不存在的空间。

You create a new ArrayList C4 , you never put anything in the List , and then you ask it for the element at index 0 . 您创建了一个新的ArrayList C4 ,您从不将任何东西放在List ,然后向其索要索引0的元素。 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 您可以通过调整方法来避免此异常

public static EntityC4 getitemfromnumber(int num)
{
  EntityC4 entity = null;
  if ( num >=0 && num < C4.size ){
    entity = (EntityC4)C4.get(num);
  }
  return entity;
}

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) 这样可以避免出现异常,但是由于您永远找不到元素(在列表中不可用),因此您会陷入循环中

C4 is obviously empty if you just call getNum() , thus the exception saying that the index you are accessing doesn't exist. 如果仅调用getNum() ,则C4显然是空的,因此该异常表示您正在访问的索引不存在。

You might want to pre-populate the list by calling createdetonater(..) ? 您可能要通过调用createdetonater(..)来预填充列表?

Your error is coming from 您的错误来自

EntityC4 c = (EntityC4)C4.get(num);

which states your C4 is empty, you would like to do a not null and notEmpty check on your list. 指出您的C4为空,您想对列表进行not nullnotEmpty检查。

In order to check the reason why C4 is empty do a trace on createdetonater and see where its being called. 为了检查C4为空的原因,请在createdetonater上进行跟踪,并查看其调用位置。

Your error message tells you everything: you're trying to get the zeroth element from a list that has no elements. 您的错误消息会告诉您所有信息:您正在尝试从没有元素的列表中获取第零个元素。 You probably need to add something to C4 , 您可能需要在C4添加一些内容,

If you rewrite your loop to use a counter then it will avoid this error. 如果重写循环以使用计数器,则可以避免此错误。 I am not sure exactly what your code is trying to do though (return first non-null instance?), so the loop below may be incorrect. 我不确定您的代码到底想做什么(返回第一个非空实例?),因此下面的循环可能不正确。

If you are trying to store a map of ID to entity, then you should probably use a HashMap<Integer,EntityC4> instead. 如果您尝试存储ID到实体的映射,则可能应该使用HashMap <Integer,EntityC4>代替。

for(int num = 0; num < C4.size(); num++)
{
    EntityC4 c = (EntityC4)C4.get(num);
    System.out.print("current num : " + num);

    if(c != null)
    {
        return num;
    } else
    {
        System.out.print("entity " + num + " is null" );
    }
}
// throw an exception or whatever else your code should do here...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM