java.lang.ArrayIndexOutOfBoundsException: length=0; index=0 - 数据库读取 - Android
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29149372/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.lang.ArrayIndexOutOfBoundsException: length=0; index=0 - Database Reading - Android
提问by Federico Belotti
I created a method that reads data from a database and puts it in a String
array.
Android Studio doesn't give syntax errors but when i launch my app the log says:
我创建了一个从数据库读取数据并将其放入String
数组的方法。Android Studio 不会给出语法错误,但是当我启动我的应用程序时,日志显示:
03-19 16:31:20.938 2518-2518/com.mms.dailypill E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.mms.dailypill, PID: 2518
java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
at com.mms.dailypill.DBManager.getMedicines(DBManager.java:56)
The method code is:
方法代码为:
public String[] getMedicines()
{
SQLiteDatabase db = dbManager.getWritableDatabase();
String[] columns = {dbManager.getName(), dbManager.getFormat(), dbManager.getAmount(), dbManager.getExp_date(), dbManager.getTime()};
Cursor cursor = db.query(dbManager.getTableName(), columns, null, null, null, null, null);
String[] medicines = {};
String name, format, exp_date, time;
int amount;
int count = 0;
while(cursor.moveToNext())
{
name = cursor.getString(0);
format = cursor.getString(1);
amount = cursor.getInt(2);
exp_date = cursor.getString(3);
time = cursor.getString(4);
medicines[count] = "Name: " + name + " Format: " + format + " Amount: " + amount + " Expiration Date: " + exp_date + " Time: " + time;
count++;
}
db.close();
return medicines;
}
As the log says, the exception is given on the line 56:
正如日志所说,第 56 行给出了异常:
time = cursor.getString(4);
The table i read has 6 columns: id
, name
, format
, amount
, exp_date
, time
. I only want to show the entire table without the id
column, so when i call the getString()
method i start from the index 1
and not from 0
.
我读过的表有 6 列:id
, name
, format
, amount
, exp_date
, time
。我只想显示没有id
列的整个表,所以当我调用该getString()
方法时,我从索引1
而不是从0
.
I really can't understand where the problem is, so if someone can help me i will appreciate it. Thanks in advance!
我真的不明白问题出在哪里,所以如果有人可以帮助我,我将不胜感激。提前致谢!
回答by Mina Fawzy
java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
even you solve your problem , but I would like to explain why this error occurred , this error happen when you initialize empty array without size , or wrong initialize in this case String[] medicines = {};
its empty with size 0 , then he try add value at first index 0 that not exist , so the solution initialize with his list size String[] medicines = new String[cursor.getCount()];
即使你解决了你的问题,但我想解释为什么会发生这个错误,当你初始化没有大小的空数组时会发生这个错误,或者错误的初始化在这种情况下String[] medicines = {};
它的大小为0,然后他尝试在第一个索引0处添加值不存在,所以解决方案用他的列表大小初始化 String[] medicines = new String[cursor.getCount()];
Notice
注意
An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed. You have seen an example of arrays already, in the main method of the "Hello World!" application. This section discusses arrays in greater detail.
数组是一个容器对象,它包含固定数量的单一类型的值。数组的长度是在创建数组时确定的。创建后,其长度是固定的。您已经在“Hello World!”的 main 方法中看到了一个数组示例。应用。本节更详细地讨论数组。
Summary
概括
one array created it take fixed size , not like array list you can add as you want.
创建的一个数组采用固定大小,而不是您可以根据需要添加的数组列表。
回答by Goodlife
for my case I had to request for the length of the array
就我而言,我不得不请求数组的长度
if (bill_inputs.length>0){
textViewInputType.setText(bill_inputs[0].getInput_name());
}
so for ur case check the length before doing anything
所以对于你的情况,在做任何事情之前检查长度