Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Protobuf + java java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0

if (hashTags!=null && hashTags.size()>0) {
        for (int i = 0; i<hashTags.size();i++){
        getPostsRequestBuilder.setTags(i, hashTags.get(i));                         
        }
    }

now, hashTags is an ArrayList<String> and it's not empty, so the problem is not in that. Tags in protobuf is a repeated string. so, if I remove getPostsRequestBuilder.setTags and do nothing - program doesn't crash, so problem is with that. it crashes on the first (0) element

12-22 15:24:25.390: E/AndroidRuntime(6252): Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
12-22 15:24:25.390: E/AndroidRuntime(6252):     at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
12-22 15:24:25.390: E/AndroidRuntime(6252):     at java.util.ArrayList.set(ArrayList.java:481)
12-22 15:24:25.390: E/AndroidRuntime(6252):     at com.google.protobuf.LazyStringArrayList.set(LazyStringArrayList.java:119)
12-22 15:24:25.390: E/AndroidRuntime(6252):     at com.google.protobuf.LazyStringArrayList.set(LazyStringArrayList.java:1)
12-22 15:24:25.390: E/AndroidRuntime(6252):     at protobuf.shrick.Shrick$GetPostsRequest$Builder.setTags(Shrick.java:20845)
12-22 15:24:25.390: E/AndroidRuntime(6252):     at pro.shrick.asynctasks.SearchRequest.doInBackground(SearchRequest.java:29)
12-22 15:24:25.390: E/AndroidRuntime(6252):     at pro.shrick.asynctasks.SearchRequest.doInBackground(SearchRequest.java:1)
like image 546
Vlad Alexeev Avatar asked Dec 10 '22 19:12

Vlad Alexeev


1 Answers

You should only use set* methods to overwrite values currently in the repeated field. Your repeated field is currently empty, so there is no 0-th element to overwrite.

Use add* methods to add new values to the repeated field:

getPostsRequestBuilder.addTags(hashTags.get(i));
like image 65
Andy Turner Avatar answered Jan 04 '23 04:01

Andy Turner