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:
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:
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
.
I really can't understand where the problem is, so if someone can help me i will appreciate it. Thanks in advance!