如何解决IndexOutOfBoundsException:索引1无效,大小为1?

问题描述:

我刚刚收到java.lang.IndexOutOfBoundsException:但无法理解它是由什么引起的。基本上,我正在将sqlite表的记录整合到listview中。 这是我的sql查询从表中获取记录。如何解决IndexOutOfBoundsException:索引1无效,大小为1?

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; 
} 

这是我的方法检索数据: -

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(); 

    } 
} 

但是,当我运行这段代码,它给了我这个错误?

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) 
+2

因为count数组的大小为1,所以你需要通过0索引来访问 – ThiepLV

首先是使用switch代替if-else分支,因为我看到你的结果从查询会给结果的修正数为数组(开关更有效,提高可读性)。

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; 
    } 

现在来到你的问题,如果你看到你的Exception堆栈跟踪和调试应用程序,一旦你会很容易让你的解决方案。

这是您的堆栈跟踪says--

java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1 

希望你知道这火爆异常IndexOutOfBoundsException只能排在你试图访问不存在于数组中的任何数组索引。

现在您的错误信息清楚地表明您的Array大小为one。这意味着您的阵列只能通过索引zero(0)访问。

所以,请调试代码,并试图找出其中的代码行产生例外,事实上

取而代之的是:

ArrayList<Integer> count = helper.getRecordsCount(1); 

试试这个:

ArrayList<Integer> count = helper.getRecordsCount(0); 
+0

不,我不能。我得到的记录使用否 – Mahesh

+0

@MaheshBpsk要在第一个位置访问元素,您应该访问元素为0.索引从0开始。您正在检查是否(不== 0)并发送1来访问记录。并在第二,而不是2把1 –

首先,你需要避免调用getRecords()那么多次。它会降低性能,并且可能不会返回相同的结果 - 可能导致错误的原因。相反,引入一个本地参数作为此函数的缓存输出并与之一起玩。

+1

这应该是一个评论。你也没有真正回答OP提出的问题。 – YoungHobbit

+2

我的声望 Robo