Home » Android » Error java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0 in my program android

Error java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0 in my program android

Posted by: admin June 15, 2020 Leave a comment

Questions:

I start to developpe in android, and I try to modify Snake code for including a call to browser for desplaying web page, when i execute my application it shows the page in the brwser, but the problem when i come back to snake application it shows message that the application has stoped unexpectedly, and the logCat show anthe exception below

02-19 01:06:50.920: W/dalvikvm(3394): threadid=1: thread exiting with uncaught exception (group=0x4015a760)
02-19 01:06:50.930: E/AndroidRuntime(3394): FATAL EXCEPTION: main
02-19 01:06:50.930: E/AndroidRuntime(3394): java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
02-19 01:06:50.930: E/AndroidRuntime(3394):     at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
02-19 01:06:50.930: E/AndroidRuntime(3394):     at java.util.ArrayList.get(ArrayList.java:308)
02-19 01:06:50.930: E/AndroidRuntime(3394):     at com.example.android.snake.SnakeView.updateSnake(SnakeView.java:448)
02-19 01:06:50.930: E/AndroidRuntime(3394):     at com.example.android.snake.SnakeView.update(SnakeView.java:403)
02-19 01:06:50.930: E/AndroidRuntime(3394):     at com.example.android.snake.SnakeView.setMode(SnakeView.java:335)
02-19 01:06:50.930: E/AndroidRuntime(3394):     at com.example.android.snake.SnakeView.onKeyDown(SnakeView.java:278)
02-19 01:06:50.930: E/AndroidRuntime(3394):     at android.view.KeyEvent.dispatch(KeyEvent.java:2387)
02-19 01:06:50.930: E/AndroidRuntime(3394):     at android.view.View.dispatchKeyEvent(View.java:4569)
02-19 01:06:50.930: E/AndroidRuntime(3394):     at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1113)
02-19 01:06:50.930: E/AndroidRuntime(3394):     at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1113)
02-19 01:06:50.930: E/AndroidRuntime(3394):     at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1113)
02-19 01:06:50.930: E/AndroidRuntime(3394):     at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1113)
02-19 01:06:50.930: E/AndroidRuntime(3394):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:1692)
02-19 01:06:50.930: E/AndroidRuntime(3394):     at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1260)
02-19 01:06:50.930: E/AndroidRuntime(3394):     at android.app.Activity.dispatchKeyEvent(Activity.java:2231)
02-19 01:06:50.930: E/AndroidRuntime(3394):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1648)
02-19 01:06:50.930: E/AndroidRuntime(3394):     at android.view.ViewRoot.deliverKeyEventPostIme(ViewRoot.java:2730)
02-19 01:06:50.930: E/AndroidRuntime(3394):     at android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2703)
02-19 01:06:50.930: E/AndroidRuntime(3394):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1958)
02-19 01:06:50.930: E/AndroidRuntime(3394):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-19 01:06:50.930: E/AndroidRuntime(3394):     at android.os.Looper.loop(Looper.java:126)
02-19 01:06:50.930: E/AndroidRuntime(3394):     at android.app.ActivityThread.main(ActivityThread.java:3997)
02-19 01:06:50.930: E/AndroidRuntime(3394):     at java.lang.reflect.Method.invokeNative(Native Method)
02-19 01:06:50.930: E/AndroidRuntime(3394):     at java.lang.reflect.Method.invoke(Method.java:491)
02-19 01:06:50.930: E/AndroidRuntime(3394):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
02-19 01:06:50.930: E/AndroidRuntime(3394):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
02-19 01:06:50.930: E/AndroidRuntime(3394):     at dalvik.system.NativeStart.main(Native Method)
02-19 01:08:07.450: D/A    
How to&Answers:

I think it’s quite clear, here

at com.example.android.snake.SnakeView.updateSnake(SnakeView.java:448)

you are accessing an ArrayList that has been instantiated as new ArrayList(0), quite straightforward if you at least read the error.

ArrayList objects are meant to automatically resize, but if you don’t place anything in a zero-sized array list then you get this error.

Answer:

You have an ArrayList with 0 elements in them. and in the SnakeView class in the updateSnake() method on line 448, you try to access the arraylist at index 0. At index 0 is the first element of you ArrayList, but if it is empty there is no element at index 0. So you get an IndexOutOfBoundsException