Sunday, 8 September 2013

Android NullPointerException while retrieving data from SQLite Database and storing it in an Array

Android NullPointerException while retrieving data from SQLite Database
and storing it in an Array

I am trying to retrieve string data from a column in database and insert
into a string array in android. I have used the following code to retrieve
data - (helper.java)
public String[] personslist() {
// TODO Auto-generated method stub
int i=0;
Cursor c=myDataBase.rawQuery("select PersonName from
Persons;",null);
String[] values = {};
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
values[i] = c.getString(c.getColumnIndex("PersonName"));
i++;
}
return values;
}
I have used the following code in "adapter.java" class to return the data
returned by personslist().
public String[] plist() {
// TODO Auto-generated method stub
String[] persons = mDbHelper.personslist(); //this is line no. 131 in
adapter class
return persons;
}
While I am running the application, it is getting crashed and showwing the
following error
09-08 08:23:19.715: E/AndroidRuntime(29373): Caused by:
java.lang.NullPointerException
09-08 08:23:19.715: E/AndroidRuntime(29373): at
com.example.fromstart.adapter.plist(adapter.java:131)
The query, when run on sqlitebrowser, it returns four rows of four persons
names. But, while running on an application, it is returning
NullPointerException. Can you please suggest me, where I might have gone
wrong?
Thanks in advance.

No comments:

Post a Comment