while (index < list.size) {
index = index + 1;
System.out.println(list.get(index));
}
Here index
is incremented before accessing the list. So it reads one element ahead everytime. So run-time error when the list is not empty.
If the list is empty then the condition while (index < list.size)
will fail and hence the loop code that causes the run-time error will never be executed.
Although not relevant to your question, the correct code would be to increment the index
after reading:
while (index < list.size) {
System.out.println(list.get(index));
index = index + 1;
}