how to solve java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0?

我的梦境 提交于 2019-12-02 06:49:49

java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0 at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)

I believe the problem is here,

 List<SuggestGetSetClientProduct> new_suggestions =jp.getParseJsonWCF(constraint.toString());

Did you initialize your ArrayList ?

List<SuggestGetSetClientProduct> new_suggestions = new ArrayList<SuggestGetSetClientProduct>;

Check the size of the ArrayList first before it goes to For loop.

EDIT :

Your code should be like,

                if (constraint != null) {

                   List<SuggestGetSetClientProduct> new_suggestions = new ArrayList<SuggestGetSetClientProduct>();
                   new_suggestions =jp.getParseJsonWCF(constraint.toString());
                   Log.d("test", "Size of array : " +new_suggestions.size());

               // rest code here..//

Also, you didn't initialize your list in SuggestionAdapterClientProduct class,

 private List<String> suggestions;

Initialize your arrayList by,

List<String> suggestions = new ArrayList<String>();

in that class.

I made some of mistakes..I solved it..and in my onitemclick..

JsonParseClientList jps=new JsonParseClientList();

             List<SuggestGetSetClientList> list =jps.getParseJsonWCF(acTextView.getText().toString());

                for(int i = 0;i<list.size();i++)
                {
                  if(list.get(i).getName().equals(acTextView.getText().toString()))
                   //params.add(new BasicNameValuePair("cid",list1.get(i).getId()));

                  catids=list.get(position).getId();



                }

                letss=catids.toString();
                System.out.println("aastala xp"+letss);
                new AttemptLogin().execute();

Why don't you try like this

List<SuggestGetSetClientProduct> new_suggestions=new ArrayList<>();

and then

new_suggestions =jp.getParseJsonWCF(constraint.toString());
user12111158

I solved this problem like this:

public class ServiceListAdapter extends RecyclerView.Adapter<ServiceListAdapter.MyViewHolder> {

    private ArrayList<Types> mtypesarray;
    Context context;
    ClickItem clickItem;

    public ServiceListAdapter(Context context, ArrayList<Types>typesarray, ClickItem clickItem) {
        this.context = context;
        this.clickItem = clickItem;
}

Change this to:

public class ServiceListAdapter extends RecyclerView.Adapter<ServiceListAdapter.MyViewHolder> {
    private ArrayList<Types> mtypesarray;
    Context context;
    ClickItem clickItem;

    public ServiceListAdapter(Context context, ArrayList<Types>typesarray, ClickItem clickItem) {
        this.context = context;
        this.clickItem = clickItem;
        mtypesarray = typesarray;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!