6

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

3
  • you cannot resize an array Commented Apr 2, 2014 at 9:19
  • 3
    length=4; index=4 length array - Array indexes are 0 based. For an array of length = 4, the index range is 0, ..., 3. An index = 4 is out of range. Commented Apr 2, 2014 at 9:22
  • You cannot resize an array, you can use an ArrayList instead. Commented Apr 2, 2014 at 9:24

2 Answers 2

4

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>();
Sign up to request clarification or add additional context in comments.

Comments

2

Array Index out of bound Exception comes when you try to access index which is not available in your Array.

You have specified. Object[] lista = (Object[]) output[2];

That's means you have two element. first is lista[0] and second is lista[1].

Now you tring to access lista[2]. It means you try to access third element which is actually not available in your Objeact[] lista. So It gives Exception at SegmentoAnterioreFragment.onTaskComplete(SegmentoAnterioreFragment.java:471)

Arraylist is dynamic arraylist. you can add and delete element directly. you not need to specify element limit.

you can use this arraylist instead of Object Array

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.