Bartosz
Bartosz

Reputation: 37

Problem to return list which contains items from rest api

I've got a problem with a method in Android which should return a list of items which are received from rest api. I am able to debug these items in that method - because I wrote declaration of this method in the onCreate method. But the problem occurs once I try to add these items (in that method) to the list, so the method could return these. I receive an error like:

Caused by: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

the method which should return items:

public List<Categories> displayDataaaa(List<Categories> categoriesList) {

    this.categoriesList = new ArrayList<Categories>();
    /
    String cat_name = "";

    // this.categoriesList.add(new Categories(1,"lkajsdflk"));
    this.categoriesList.add(new Categories(1, categoriesList.get(0).getCategoriesName()));


    for (int i = 0; i < categoriesList.size(); i++) {
         cat_name = categoriesList.get(i).getCategoriesName();
      //    this.categoriesList.add(new Categories(i, cat_name));
        Log.d("displayDataaaaa-cat_name", "displayDataaaaa-cat_name" + cat_name);

        this.categoriesList.addAll(categoriesList);
    }
 //   this.categoriesList.add(new Categories(1, cat_name));

    return this.categoriesList;
}

Could I ask you for some hint, please? Thank you.

Log:

 java.lang.RuntimeException: Unable to start activity ComponentInfo{dev4passion.partyj10/dev4passion.partyj10.MainActivity}: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6669)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
 Caused by: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
    at java.util.ArrayList.get(ArrayList.java:437)
    at dev4passion.partyj10.MainActivity.displayDataaaa(MainActivity.java:189)
    at dev4passion.partyj10.MainActivity.onCreate(MainActivity.java:94)
    at android.app.Activity.performCreate(Activity.java:7136)
    at android.app.Activity.performCreate(Activity.java:7127)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) 
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:193) 
    at android.app.ActivityThread.main(ActivityThread.java:6669) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 

Upvotes: 0

Views: 44

Answers (1)

Oleg Cherednik
Oleg Cherednik

Reputation: 18255

this.categoriesList.add(new Categories(1, categoriesList.get(0).getCategoriesName()));

This is your problem line. categoriesList.get(0) will throw an exception when categoriesList is empty.

Upvotes: 1

Related Questions