how to fix java.lang.IndexOutOfBoundsException

后端 未结 7 1321
长发绾君心
长发绾君心 2020-12-10 23:58

Exception in thread \"main\" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at java.util.ArrayList.rangeCheck(ArrayList.java:604)

相关标签:
7条回答
  • 2020-12-11 00:44

    You are trying to access the first element lstpp.get(0) of an empty array. Just add an element to your array and check for !lstpp.isEmpty() before accessing an element

    0 讨论(0)
  • 2020-12-11 00:47

    Use if(index.length() < 0) for Integer

    or

    Use if(index.equals(null) for String

    0 讨论(0)
  • 2020-12-11 00:48

    This error happens because your list lstpp is empty (Nothing at index 0). So either there is a bug in your getResult() function, or the empty list is normal and you need to handle this case (By checking the size of the list before, or catching the exception).

    0 讨论(0)
  • 2020-12-11 00:52

    You do not have any elements in the list so can't access the first element.

    0 讨论(0)
  • 2020-12-11 00:54

    You want to get an element from an empty array. That's why the Size: 0 from the exception

    java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

    So you cant do lstpp.get(0) until you fill the array.

    0 讨论(0)
  • 2020-12-11 00:55

    lstpp is empty. You cant access the first element of an empty list.

    In general, you can check if size > index.

    In your case, you need to check if lstpp is empty. (you can use !lstpp.isEmpty())

    0 讨论(0)
提交回复
热议问题