-5

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

JRE
  • 81
  • 1
  • 1
  • 5
  • index > size should do – Ankit Sep 16 '13 at 08:45
  • @ay89 that is java library code you are criticizing. – Karthik T Sep 16 '13 at 08:46
  • 2
    try to find problem in `getResult` method. – turbanoff Sep 16 '13 at 08:47
  • All the information you need is in this question.. the answers arent going to be able to provide any breakthroughs, perhaps you can read the question once and you should get it. – Karthik T Sep 16 '13 at 08:47
  • @ay89 false. That would just make things entirely wrong... Think about that again. – ppeterka Sep 16 '13 at 08:47
  • @KarthikT umm.. no, threw just a solution in general. haven't noticed the context. – Ankit Sep 16 '13 at 08:48
  • @ay89 no, you just wrote random garbage without reading. And it is **not a solution**. With or without context... – ppeterka Sep 16 '13 at 08:51
  • @ppeterka66 see the answer of whoAmI, its exactly the same, except of the context of ArrayList.java. – Ankit Sep 16 '13 at 08:53
  • 1
    @ay89 your answer `index>size would do` is still **garbage, false, and misleading**, no matter who you refer to. **Also, if you could read properly what zou referenced, you'd notice the relation operator to be the _other way round_...** Show some respect, and admit your own faults. That is crucial in professional environments. Sorry for shouting, but spreading so obviously bad information, and trying to defend it so fiercely brings out the animal from me. – ppeterka Sep 16 '13 at 08:56
  • See https://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it – Raedwald Jan 22 '20 at 17:21
  • Does this answer your question? [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](https://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it) – Alexei Levenkov Jan 22 '20 at 18:24

7 Answers7

6

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.

DavidM
  • 570
  • 4
  • 21
5

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())

BobTheBuilder
  • 18,858
  • 6
  • 40
  • 61
2

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

svarog
  • 9,477
  • 4
  • 61
  • 77
Prasad Kharkar
  • 13,410
  • 5
  • 37
  • 56
2
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++){
...}
Alex K
  • 22,315
  • 19
  • 108
  • 236
Ranjeet
  • 21
  • 1
0

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

phoenix7360
  • 2,807
  • 6
  • 30
  • 41
0

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).

Kaidjin
  • 1,433
  • 1
  • 12
  • 18
  • Thanks sir, i've found the trouble is in the getResult() function, forget to change the "Return null" to "return (the variable)". *I'm a java beginner* xoxo – JRE Sep 17 '13 at 04:29
0

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

or

Use if(index.equals(null) for String

eyllanesc
  • 235,170
  • 19
  • 170
  • 241