Home java IndexOutOfBoundsException when accessing ArrayList

IndexOutOfBoundsException when accessing ArrayList

Author

Date

Category

Why the application crashes when using the function

private static ArrayList & lt; ArrayList & lt; String & gt; & gt; cacheTemp = null;
public static boolean setReturnArrayList (int comnat, ArrayList & lt; String & gt; temp) {
    if (cacheTemp == null) return false;
    cacheTemp.set (comnat, temp); // LINE WHERE THE FALL OCCURS
    return true;
  }

For work I use static cacheTemp I use functions

public static boolean load (Context context) {
  cacheTemp = new ArrayList & lt; ArrayList & lt; String & gt; & gt; ();
  cacheTemp.clear ();
  // SHORT VERSION FOR QUESTION !!!
}
public static boolean regenCache (int vsegocomnat) {
  if (cacheTemp == null) return false;
  cacheTemp.clear ();
  ArrayList & lt; String & gt; temp = new ArrayList & lt; String & gt; ();
  for (int i = 0; i & lt; vsegocomnat; i ++)
  {
    setReturnArrayList (i, temp); // HERE FALLING GOES TO FUNCTION (ABOVE)
  }
  return true;
}

Then I use it where I need it

MessagerCache.load (context); // INITIALIZE (SHORT OPTION)
  MessagerCache.regenCache (10); // FALL HERE
  Voider.Log (context, "getLengthComnat", String.valueOf (MessagerCache.getLengthComnat ())); //DOES NOT MATTER
  MessagerCache.save (); //DOES NOT MATTER

Here are the logs

08-19 12: 24: 51.707 11092-11092 / ux.uchat E / AndroidRuntime ﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo {ux.uchat / ux.uchat.Messager}: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2306)
at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2358)
at android.app.ActivityThread.access $ 600 (ActivityThread.java:156)
at android.app.ActivityThread $ H.handleMessage (ActivityThread.java:1340)
at android.os.Handler.dispatchMessage (Handler.java:99)
at android.os.Looper.loop (Looper.java:153)
at android.app.ActivityThread.main (ActivityThread.java:5299)
at java.lang.reflect.Method.invokeNative (Native Method)
at java.lang.reflect.Method.invoke (Method.java:511)
at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:600)
at de.robv.android.xposed.XposedBridge.main (XposedBridge.java:132)
at dalvik.system.NativeStart.main (Native Method)
Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
at java.util.ArrayList.throwIndexOutOfBoundsException (ArrayList.java:251)
at java.util.ArrayList.set (ArrayList.java:477)
at ux.uchat.MessagerCache.setReturnArrayList (MessagerCache.java:84)
// POINTS TO LINE (cacheTemp.set (comnat, temp); // LINE WHERE FALLS)
at ux.uchat.MessagerCache.regenCache (MessagerCache.java:68)
at ux.uchat.MessagerVoider.UpdateListChat (MessagerVoider.java:41)
at ux.uchat.Messager.onCreate (Messager.java:25)
at android.app.Activity.performCreate (Activity.java:5122)
at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1081)
at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2270)
at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2358)
at android.app.ActivityThread.access $ 600 (ActivityThread.java:156)
at android.app.ActivityThread $ H.handleMessage (ActivityThread.java:1340)
at android.os.Handler.dispatchMessage (Handler.java:99)
at android.os.Looper.loop (Looper.java:153)
at android.app.ActivityThread.main (ActivityThread.java:5299)
at java.lang.reflect.Method.invokeNative (Native Method)
at java.lang.reflect.Method.invoke (Method.java:511)
at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:600)
at de.robv.android.xposed.XposedBridge.main (XposedBridge.java:132)
at dalvik.system.NativeStart.main (Native Method)


Answer 1, authority 100%

java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0 – indicates that you are trying to access an array element that does not exist. You are trying to replace the element at index 0, but it does not exist yet. The size of the array is 0. To add an element, use the add () method.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions