Carlos Manzo Carlos Manzo - 4 months ago 36
Android Question

java.lang.ArrayIndexOutOfBoundsException: length=4; index=4 length array

i have this error:

04-02 11:03:57.922: E/AndroidRuntime(18952): FATAL EXCEPTION: main
04-02 11:03:57.922: E/AndroidRuntime(18952): java.lang.ArrayIndexOutOfBoundsException: length=4; index=4
04-02 11:03:57.922: E/AndroidRuntime(18952): at it.nad.cartellecliniche.fragment.SegmentoAnterioreFragment.onTaskComplete(SegmentoAnterioreFragment.java:471)
04-02 11:03:57.922: E/AndroidRuntime(18952): at it.nad.cartellecliniche.asynctask.LookupTask.onPostExecute(LookupTask.java:279)
04-02 11:03:57.922: E/AndroidRuntime(18952): at it.nad.cartellecliniche.asynctask.LookupTask.onPostExecute(LookupTask.java:1)
04-02 11:03:57.922: E/AndroidRuntime(18952): at android.os.AsyncTask.finish(AsyncTask.java:631)
04-02 11:03:57.922: E/AndroidRuntime(18952): at android.os.AsyncTask.access$600(AsyncTask.java:177)
04-02 11:03:57.922: E/AndroidRuntime(18952): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
04-02 11:03:57.922: E/AndroidRuntime(18952): at android.os.Handler.dispatchMessage(Handler.java:99)
04-02 11:03:57.922: E/AndroidRuntime(18952): at android.os.Looper.loop(Looper.java:137)
04-02 11:03:57.922: E/AndroidRuntime(18952): at android.app.ActivityThread.main(ActivityThread.java:5414)
04-02 11:03:57.922: E/AndroidRuntime(18952): at java.lang.reflect.Method.invokeNative(Native Method)
04-02 11:03:57.922: E/AndroidRuntime(18952): at java.lang.reflect.Method.invoke(Method.java:525)
04-02 11:03:57.922: E/AndroidRuntime(18952): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
04-02 11:03:57.922: E/AndroidRuntime(18952): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
04-02 11:03:57.922: E/AndroidRuntime(18952): at dalvik.system.NativeStart.main(Native Method)


this is the code:

else if (typeTask.equals("LookupSegmentoAnteriore")) {

switch (resultOperation) {
case 200: {
Object[] lista = (Object[]) output[2];

listaBulbo = (String[])lista[0];
Arrays.sort(listaBulbo);
listaApparatoLacrimale = (String[])lista[1];
Arrays.sort(listaApparatoLacrimale);
listaPalpebre = (String[])lista[2];
Arrays.sort(listaPalpebre);
listaCongiuntiva = (String[])lista[3];
Arrays.sort(listaCongiuntiva);
listaCornea = (String[])lista[4];
Arrays.sort(listaCornea);
listaAngolo = (String[])lista[5];
Arrays.sort(listaAngolo);
listaCamera_anteriore = (String[])lista[6];
Arrays.sort(listaCamera_anteriore);
listaIride = (String[])lista[7];
Arrays.sort(listaIride);
listaPupilla = (String[])lista[8];
Arrays.sort(listaPupilla);
listaCristallino = (String[])lista[9];
Arrays.sort(listaCristallino);
break;
}
}
}


i have added new elements to the list starting

from:

listaCornea = (String[])lista[4];
Arrays.sort(listaCornea);


to:

listaCristallino = (String[])lista[9];
Arrays.sort(listaCristallino);


somebody can help me to resolve this problem? i don't know how to manage the array length problem. I just added the new elements to the list, but i don't know if this is the right form to do it. Do i need to resize my array? how i can do that?

thanks a lot

Answer

problem is your array may declared to have only 4 rooms, that means you can access maximum index of 3 but in above code you gonna access 4th index, then it will complain that your index bound in out,your lenth is 4,

you should use ArrayList instead of array,because Array can't be resize after declaration, but you can add any number of objects to arraylist.

ArrayList<Object> lista=new ArrayList<Object>();