java.lang.ArrayIndexOutOfBoundsException:长度= 0;指数= 0 - 读取数据库 - Android电子 [英] java.lang.ArrayIndexOutOfBoundsException: length=0; index=0 - Database Reading - Android

查看:161
本文介绍了java.lang.ArrayIndexOutOfBoundsException:长度= 0;指数= 0 - 读取数据库 - Android电子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个从数据库中读取数据,并将其在字符串阵列的方法。
Android的工作室不给语法错误,但是当我启动我的应用程序日志说:

  03-19 16:31:20.938 2518年至2518年/ com.mms.dailypill E / AndroidRuntime:致命异常:主要
工艺:com.mms.dailypill,PID:2518
java.lang.ArrayIndexOutOfBoundsException:长度= 0;指数= 0
        在com.mms.dailypill.DBManager.getMedicines(DBManager.java:56)

方法code是:

 公共字符串[] getMedicines()
{
    SQLiteDatabase分贝= dbManager.getWritableDatabase();
    的String [] =列{dbManager.getName(),dbManager.getFormat(),dbManager.getAmount(),dbManager.getExp_date(),dbManager.getTime()};
    光标光标= db.query(dbManager.getTableName(),列,NULL,NULL,NULL,NULL,NULL);
    的String []药品= {};    字符串名称,格式,EXP_DATE,时间;    INT金额;
    诠释计数= 0;
    而(cursor.moveToNext())
    {
        名称= cursor.getString(0);
        格式= cursor.getString(1);
        量= cursor.getInt(2);
        EXP_DATE = cursor.getString(3);
        时间= cursor.getString(4);
        药品[统计] =名称:+姓名+格式:+格式+金额+量+到期日期:+ EXP_DATE +时间:+时间;
        算上++;
    }
    db.close();
    返回的药品;
}

作为日志说,该异常是上线56给出:

 时间为cursor.getString(4);

我读表有6列: ID 名称格式金额 EXP_DATE 时间 。我只是想显示整个表,而不 ID 列,因此当我打电话的getString()方法我开始从指数 1 ,而不是从 0

我真的不明白问题出在哪里,因此,如果有人能帮助我,我会AP preciate它。
在此先感谢!


解决方案

  java.lang.ArrayIndexOutOfBoundsException:长度= 0;指数= 0

即使你解决你的问题,但我想解释为什么发生这个错误,当你初始化空数组没有大小这个错误发生,还是错在这种情况下初始化的String []药品= {} ; 的空大小为0,那么他试图从那个不存在的第一个索引0值,所以用他的列表大小的String []药品=新的String [光标解决初始化.getCount();

的通知

数组是保存单个类型的值的一个固定数目的容器对象。创建阵列时的阵列的长度被确定。创建后,它的长度是固定的。你见过一个数组的例子已经在主要方法的Hello World!应用。本节讨论更详细的数组。

摘要

一个阵列中创建它采取固定的大小,而不是像数组列表,只要你想,你可以添加。

来源

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!

解决方案

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

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.

Summary

one array created it take fixed size , not like array list you can add as you want.

Source

这篇关于java.lang.ArrayIndexOutOfBoundsException:长度= 0;指数= 0 - 读取数据库 - Android电子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆