【问题标题】:How to resolve IndexOutOfBoundsException: Invalid index 1, size is 1? [duplicate]如何解决 IndexOutOfBoundsException:索引 1 无效,大小为 1? [复制]
【发布时间】:2016-10-09 13:55:48
【问题描述】:

我刚刚收到 java.lang.IndexOutOfBoundsException: 但不明白它是由什么引起的。基本上,我正在将 sqlite 表的记录集成到列表视图中。 这是我从表中获取记录的 sql 查询。

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)

【问题讨论】:

  • 因为count数组大小为1所以需要按0索引访问

标签: java android sqlite arraylist


【解决方案1】:

首先使用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 StackTrace 并在您轻松获得解决方案后调试应用程序,现在就来解决您的问题。

这就是你的堆栈跟踪所说的--

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

希望您知道这个流行的异常 IndexOutOfBoundsException 仅在您尝试访问数组中不存在的任何数组索引时出现。

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

所以,请调试您的代码并尝试找出实际上是哪一行代码产生了异常

【讨论】:

    【解决方案2】:

    而不是这个:

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

    试试这个:

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

    【讨论】:

    • 不,我不能。我正在使用那个 no 获取记录
    • @MaheshBpsk 要访问第一个位置的元素,您应该访问 0 处的元素。索引从 0 开始。您正在检查是否 (no == 0) 并发送 1 以访问记录。并且在第二个 if 而不是 2 中放 1.
    【解决方案3】:

    首先,您需要避免多次调用 getRecords()。它会降低性能并且可能不会返回相同的结果——这可能是您出错的原因。相反,引入一个本地参数作为此函数的缓存输出并使用它。

    【讨论】:

    • 这应该是一条评论。此外,您实际上并没有回答 OP 提出的问题。
    • 我的声誉
    猜你喜欢
    • 1970-01-01
    • 2018-02-13
    • 1970-01-01
    • 2013-10-28
    • 2019-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多