5

So the error message is this:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at FcfsScheduler.sortArrival(FcfsScheduler.java:77)
at FcfsScheduler.computeSchedule(FcfsScheduler.java:30)
at ScheduleDisks.main(ScheduleDisks.java:33)

with my code as

public void sortArrival(List<Request> r)
{
    int pointer = 0;
    int sProof = 0;
    while(true)
    {
        if(r.get(pointer).getArrivalTime()<r.get(pointer+1).getArrivalTime())
        {
            Request r1 = r.get(pointer);
            Request r2 = r.get(pointer+1);
            r.set(pointer, r2);
            r.set(pointer+1, r1);
        }
        else
        {
            sProof++;
        }
        ++pointer;
        if(pointer>r.size()-2)
        {
            pointer=0;
            sProof=0;
        }
        if(sProof>=r.size()-2)
        {
            break;
        }
    }
}

The error is at

if(r.get(pointer).getArrivalTime()<r.get(pointer+1).getArrivalTime())

but I think the array index is checked ok with the code after the increment of pointer. Is it an array out of bounds exception or something else? Normally, the error is ArrayIndexOutOfBoundsException when it is the array. What seems to be the problem here?

jww
  • 97,681
  • 90
  • 411
  • 885
  • 7
    This error is showing because your `ArrayList` is empty.!! – Vishal K Mar 17 '13 at 15:41
  • Try debugging your code using debugger, you can get exactly where and how error is occurring. – Pradeep Simha Mar 17 '13 at 15:41
  • you should check pointer+1 against the size of r in your while decleration to avoid this error. also, checking that r is not null and not empty may be a good idea. – Oren Mar 17 '13 at 15:45
  • List you have input is empty – andy Mar 17 '13 at 16:00
  • This is tangential to the exception, but if you are trying to sort your List, a better approach is having the Request class implements [Comparable](http://docs.oracle.com/javase/6/docs/api/java/lang/Comparable.html) instead. You could then use Collections.sort(r). – Ibrahim Arief Mar 17 '13 at 16:05
  • 1
    Possible duplicate of [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](https://stackoverflow.com/q/5554734/608639) – jww Jun 12 '19 at 15:47

3 Answers3

6

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

ArrayList is empty. It does not contain any element.

Index: 0, Size: 0.

You are trying to access it.So you are getting IndexOutOfBoundsException.

if(r.size() == 0) && r.size() < pointer + 1)   //If ArrayList size is zero then simply return from method.
  return;
Ajay S
  • 48,003
  • 27
  • 91
  • 111
5

You are passing in an empty array. You should do some validation on the inputs

if (r == null || r.size()==0){
   throw new RuntimeException("Invalid ArrayList");
}
Raunak Agarwal
  • 7,117
  • 6
  • 38
  • 62
0

Your array size is 0, you should initialize it correctly in order to iterate it.

mkazma
  • 572
  • 3
  • 11
  • 29