Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at java.util.ArrayList.rangeCheck(ArrayList.java:604)
in line of arraylist.java
private void rangeCheck(int index) {
if (index >= size)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
in line
List lstpp = getResult(pp) ;
System.out.println("=====Persegi Panjang====");
System.out.println("luas = "+((Integer)lstpp.get(0)));
Please help
This question is related to
java
web-services
arraylist
You do not have any elements in the list so can't access the first element.
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
for ( int i=0 ; i<=list.size() ; i++){
....}
By executing this for loop , the loop will execute with a thrown exception as IndexOutOfBoundException
cause, suppose list size is 10 , so when index i will get to 10 i.e when i=10 the exception will be thrown cause index=size
, i.e. i=size
and as known that Java considers index starting from 0,1,2...etc the expression which Java agrees upon is index < size
. So the solution for such exception is to make the statement in loop as i<list.size()
for ( int i=0 ; i<list.size() ; i++){
...}
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.
Use if(index.length() < 0)
for Integer
or
Use if(index.equals(null)
for String
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).
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()
)
Source: Stackoverflow.com