gpt4 book ai didi

java - 如何解决IndexOutOfBoundsException : Invalid index 1,大小为1?

转载 作者:行者123 更新时间:2023-12-01 18:04:33 26 4
gpt4 key购买 nike

我刚刚收到 java.lang.IndexOutOfBoundsException: 但无法理解其原因。基本上,我将 sqlite 表的记录集成到 ListView 中。这是我从表中获取记录的 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)

最佳答案

首先使用 switch 而不是 if-else 分支,因为我从查询中看到的结果将以数组形式给出固定数量的结果(switch 是更高效并提高可读性)。

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

this is what your stacktrace says--

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

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

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

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

关于java - 如何解决IndexOutOfBoundsException : Invalid index 1,大小为1?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37717047/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com