How to resolve IndexOutOfBoundsException: Invalid index 1, size is 1?
Solution 1
first of all use switch
instead of if-else
branching , because what i see your Result from query will give the fix number of result as an array (switch is more efficient and improve readability) .
public void setDataInList(int no) { //just a suggestion not a answer
switch (no){
case 0 :
//Your Code as you specified in your code context.
break;
case 1 :
//Your Code as you specified in your code context.
break;
case 2 :
//Your Code as you specified in your code context.
break;
default :
break;
}
Now Coming to your problem if you see your Exception
StackTrace and debug the application once you will get your solution easily.
this is what your stacktrace says--
java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
Hope you know this popular exception IndexOutOfBoundsException
only came while you try to access the any array index which is not present in array.
Now your error message clearly said that your Array
size is one
. It means your array will only accessible to index zero
(0).
So, Please debug your code and try to find out which line of code is generating exception in-fact
Solution 2
Instead of this :
ArrayList<Integer> count = helper.getRecordsCount(1);
Try this :
ArrayList<Integer> count = helper.getRecordsCount(0);
Solution 3
First, you need to avoid calling getRecords() so many times. It costs performance and might not be returning the same result - the possible reason for your error. Instead, introduce a local parameter as a cache output of this function and play with it.
Mahesh
Updated on June 17, 2022Comments
-
Mahesh almost 2 years
i've just recived java.lang.IndexOutOfBoundsException: but can't understand what its caused from. Basically, i am integrating records of sqlite table into listview. Here is my sql query to fetch records from the table.
public ArrayList<Integer> getRecordsCount(int no) { ArrayList<Integer> count = new ArrayList<>(); Cursor c = database.rawQuery( "select count(*) as TotalCount, tDate, " + "strftime('%d', tDate) as DayPart, " + "strftime('%m', tDate) as MonthPart, " + "strftime('%Y', tDate) as YearPart " + "from library " + "where type = '" + no + "' " + "group by MonthPart, YearPart", null); while (c.moveToNext()) { count.add(c.getInt(0)); } return count; }
This is my method to retrieve that data :-
public void setDataInList(int no) { if (no == 0) { ArrayList<Integer> count = helper.getRecordsCount(1); mainGetLibraryModels.clear(); MainLibraryModelWS modelWS = helper.getAllRecordsMonthWise2(no); for (int i = 0; i < modelWS.getRecords().size(); i++) { WSLibraryModel model = new WSLibraryModel(); model.setAmount(modelWS.getRecords().get(i).getAmount()); model.setTotalCount("" + count.get(i)); model.setYearPart(modelWS.getRecords().get(i).getYearPart()); model.setMonthPart(modelWS.getRecords().get(i).getMonthPart()); mainGetLibraryModels.add(model); } adapter.notifyDataSetChanged(); } else if (no == 1) { ArrayList<Integer> count = helper.getRecordsCount(2); mainGetLibraryModels.clear(); MainLibraryModelWS modelWS = helper.getAllRecordsMonthWise2(no); for (int i = 0; i < modelWS.getRecords().size(); i++) { WSLibraryModel model = new WSLibraryModel(); model.setAmount(modelWS.getRecords().get(i).getAmount()); model.setTotalCount("" + count.get(i)); model.setYearPart(modelWS.getRecords().get(i).getYearPart()); model.setMonthPart(modelWS.getRecords().get(i).getMonthPart()); mainGetLibraryModels.add(model); } adapter.notifyDataSetChanged(); } else { mainGetLibraryModels.clear(); MainLibraryModelWS modelWS = helper.getAllRecordsMonthWise2(no); for (int i = 0; i < modelWS.getRecords().size(); i++) { WSLibraryModel model = new WSLibraryModel(); model.setAmount(modelWS.getRecords().get(i).getAmount()); model.setTotalCount(" - "); model.setYearPart(modelWS.getRecords().get(i).getYearPart()); model.setMonthPart(modelWS.getRecords().get(i).getMonthPart()); mainGetLibraryModels.add(model); } adapter.notifyDataSetChanged(); } }
But, when i run this code, it gives me this error?
FATAL EXCEPTION: main Process: com.teezom, PID: 14168 java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1 at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255) at java.util.ArrayList.get(ArrayList.java:308)