java.lang.ArrayIndexOutOfBoundsException: length=0; index=0 - Database Reading - Android

痴心易碎 提交于 2019-11-29 07:10:46
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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!