Trying to write a check for a specific group in the list:

for(Groupss g :jsonResponse.getForm().getmGroupss()) { if(g.getmControls().get(0).getTitle().equals("type")) { mList.add(g); } 

Knocks out the following error:

E / AndroidRuntime: FATAL EXCEPTION: main Process: com.comp.secondproject, PID: 11587 java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0

And points to the 2nd line in the code.

  • So the "jsonResponse" structure is empty - Valera Kvip
  • it's hard to say what getmControls () returns. If it returns a Collection, then check first for emptiness and then reach the item with the index 0 - Artem Konovalov
  • @Valera Kvip not, because Initially there was such an entry: mList.addAll (jsonResponse.getForm (). getmGroupss ()); and got great items. - Morozov
  • The error text unequivocally indicates that you are trying to access the first element of the empty collection; there can be no other here. Why it is empty can only understand you. - temq
  • @temq why initially everything worked then? Or maybe you will advise to correct something in the code? since I don’t really understand why this is happening. - Morozov

1 answer 1

change your condition to

 if(g.getmControls() != null && g.getmControls().size() > 0 && "type".equals(g.getmControls().get(0).getTitle())) 
  • Error disappeared, but list items are not displayed in my recyclerView (empty) - Morozov
  • @VadimMorozov, apparently, not one of the elements does not pass this condition. Not one in g.getmControls (). Get (0). GetTitle () no "type" - Vladyslav Matviienko
  • must have been previously displayed. Or am I looking "wrong" again?) - Morozov
  • and if for example I want to delete an item from the list? how can this element be defined? then write something like this: Iterator <Object> i = objs.iterator (); while (i.hasNext ()) {Object o = i.next (); i.remove (); - Morozov
  • @VadimMorozov, in your previous question you didn’t compare with "type" . switch (mList.get(position).getmControls().get(0).getTitle()){ case "textbox_numeric": temp=0; break; case "drop_down_options": temp = 1; break; default: temp =1; } switch (mList.get(position).getmControls().get(0).getTitle()){ case "textbox_numeric": temp=0; break; case "drop_down_options": temp = 1; break; default: temp =1; } - Vladyslav Matviienko