Strange NullPointerException error in android when using custom suggestions provider - java

I am trying to make custom search suggestions in my app. I’ve started from documentation and Searchable dictionary example. However, this example isn’t so good for me so I’ve started with some tests to find out exactly how to make it, because there is not much tutorials in the Internet also.
Generally my app has right now 2 databases – one normal and second with less number of columns – FTS3. What I would like to achieve is to connect suggestions provider to this FTS3 table.
What I was trying to do was to now was, using simple function, return in suggestions whole DB (around 200 records) after typing any letter in search box. I know about limit 50 records, but I don’t think it is the problem.
This are fragments from Provider’s code. What I found out, that when you type in text, provider goes to option SEARCH_SUGGEST:
// UriMatcher stuff
private static final int SEARCH_WORDS = 0;
private static final int GET_WORD = 1;
private static final int SEARCH_SUGGEST = 2;
private static final int REFRESH_SHORTCUT = 3;
private static final UriMatcher mUriMatcher = buildUriMatcher();
/**
* Builds up a UriMatcher for search suggestion and shortcut refresh queries.
*/
private static UriMatcher buildUriMatcher() {
Log.d(TAG,"urimatcher");
UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
// to get definitions...
matcher.addURI(AUTHORITY, "mydb", SEARCH_WORDS);
matcher.addURI(AUTHORITY, "mydb/#", GET_WORD);
// to get suggestions...
matcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY, SEARCH_SUGGEST);
matcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY + "/*", SEARCH_SUGGEST);
return matcher;
}
#Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
switch (mUriMatcher.match(uri)) {
case SEARCH_SUGGEST:
Log.d(TAG,"SEARCH_SUGGEST");
if (selectionArgs == null) {
throw new IllegalArgumentException(
"selectionArgs must be provided for the Uri: " + uri);
}
return getSuggestions(selectionArgs[0]);
case SEARCH_WORDS:
Log.d(TAG,"SEARCH_WORDS");
if (selectionArgs == null) {
throw new IllegalArgumentException(
"selectionArgs must be provided for the Uri: " + uri);
}
return search(selectionArgs[0]);
case GET_WORD:
Log.d(TAG,"GET_WORD");
return null;
default:
Log.d(TAG,"default");
throw new IllegalArgumentException("Unknown Uri: " + uri);
}
}
private Cursor getSuggestions(String query) {
String[] columns = { MyDBAdapter.KEY_TITLE,MyDBAdapter.KEY_ID};
Log.d(TAG,"query1: " + query);
try{
Cursor tmp = MyDB.getAllEntriesFTS(false, columns,
null, null, null, null, MyDBAdapter.KEY_TITLE, null, query);
Log.d(TAG,"cursor: " + Integer.toString(tmp.getCount()));
}
catch(Exception e)
{
Log.d(TAG,e.toString());
}
return tmp;
}
In getSuggestions I put code that should generally work, but it doesn’t. Doesn’t work only when used here. When I used it in other activity to get cursor for listview everything was fine. Here it returns my NullPointerException.
So getting deeper I put also some Log tags in getAllEntriesFTS method and this method looks like this:
public Cursor getAllEntriesFTS(boolean distinct, String[] result_columns,
String selection, String[] selectionArgs, String groupBy,
String having, String orderBy, String limit, String query) {
Log.d(TAG,"query db: " + query);
String[] columns = { MyDBAdapter.KEY_TITLE, MyDBAdapter.KEY_ID};
Log.d(TAG,"columns: " + Integer.toString(result_columns.length));
Cursor allRows = null;
try{
allRows = db.query(distinct, DATABASE_TABLE_FTS, columns,
null, null, null, null, MyDBAdapter.KEY_TITLE, null);
Log.d(TAG,"OK");
}
catch(Exception e)
{
Log.d(TAG, e.toString());//it always goes there with NullPointerExceptionwhen used in provider
}
Log.d(TAG,Integer.toString(allRows.getCount()));
return allRows;
}
So, generalny speaking it should return cursor to whole DB, but instead it throws In place where it shouldn’t NullPointerException.
Can someone please tell me what am I doing wrong and how it should be done?

Thank's to JB Nizet I was able to find my mistake. I was thinking I've studied Google's example good, but I was wrong.
The problem was lack of database open before cursor call. It should look like this:
private Cursor getSuggestions(String query) {
String[] columns = { MyDBAdapter.KEY_TITLE,MyDBAdapter.KEY_ID};
Log.d(TAG,"query1: " + query);
try{
MyDB.open();
Cursor tmp = MyDB.getAllEntriesFTS(false, columns,
null, null, null, null, MyDBAdapter.KEY_TITLE, null, query);
MyDB.close();
Log.d(TAG,"cursor: " + Integer.toString(tmp.getCount()));
}
catch(Exception e)
{
Log.d(TAG,e.toString());
}
return tmp;
}
Thank you all for showing me it.

Related

cursor is crashing with CursorIndexOutOfBoundsException

My cursor is crashing my application with the android database error.
CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1
I made another much less optimized slider that scans my database and I find the good value.
public Cursor getAllDataTableStaffDatabase(String table_name){
this.open();
Cursor result =this.mDb.rawQuery("SELECT * FROM " + table_name,null);
return result;// fonctionne très bien
}
public String findNameOfStaffBymail(String mail) {
String sql = " SELECT * FROM " + DatabaseStaffHandler.STAFF_TABLE_NAME + " WHERE " + DatabaseStaffHandler.STAFF_MAIL + " = ? ";
Cursor result = super.mDb.rawQuery(sql, new String[]{mail});
Cursor data = super.getAllDataTableStaffDatabase(DatabaseStaffHandler.STAFF_TABLE_NAME);
String test = result.getString(1); //error
while (data.moveToNext()) {
if (data.getString(3).equals(mail)) {
viewAll();
return data.getString(1);
}
}
}
I would like to retrieve the value name that corresponds to the email address.
This usually happens when you do not have the data in your Cursor and you are still trying to access the data. It is similar to the ArrayIndexOutOfBoundsException. I found nothing wrong with your query so far. However, I think you might consider adding null checking in your code which will prevent your application from crashing. Especially in the while loop, you need to put a null check in the condition.
And you need to use the moveToFirst function wherever necessary.
public Cursor getAllDataTableStaffDatabase(String table_name) {
this.open();
Cursor result = this.mDb.rawQuery("SELECT * FROM " + table_name,null);
return result;
}
public String findNameOfStaffBymail(String mail) {
String sql = " SELECT * FROM " + DatabaseStaffHandler.STAFF_TABLE_NAME + " WHERE " + DatabaseStaffHandler.STAFF_MAIL + " = ? ";
Cursor result = super.mDb.rawQuery(sql, new String[]{mail});
Cursor data = super.getAllDataTableStaffDatabase(DatabaseStaffHandler.STAFF_TABLE_NAME);
// Add a null checking here.
if (result != null) {
result.moveToFirst();
String test = result.getString(1);
}
if(data != null) data.moveToFirst();
while (data != null) {
if (data.getString(3).equals(mail)) {
viewAll();
return data.getString(1);
}
data.moveToNext();
}
}
Hope that solves your problem.
Thank you for your reply. I found my problem thanks to you. I wonder if the cursor does not boot at the end. But it is good practice to check if received is not null. Thank you and have a nice day

How to sort or reorder MergeCursor for ListViewAdapter

I am developing a simple social media for my case study. I was able to retrieve the post from the people the user follows. Here's the screenshot:
As you can see, the problem is that the posts were not sorted according to date/id. Instead, it is sorted according to the people the user follows. It is because I am only merging the cursors using mergecursor. Here's a part of my code:
ListView listviewFeed = (ListView) findViewById(R.id.listviewFeed);
Cursor cursorFeed = DataAdapters.getFeed(dbHelper, strUserID);
//This code is for retrieving user's own posts
Cursor cursorFollowing = DataAdapters.getFollowing(dbHelper,strUserID);
//This code is for retrieving the followed users.
if(cursorFollowing.getCount()>0) {
for (int intCtr = 0; intCtr < cursorFollowing.getCount(); intCtr++) {
int intUseridI = cursorFollowing.getColumnIndex(Tables.FollowTable.COLUMN_USERID);
String strUseridI = cursorFollowing.getString(intUseridI);
Cursor cursorFollowingFeed = DataAdapters.getFeed(dbHelper, strUseridI);
\\This code is for retrieving the posts of the people the user follows.
if(intCtr>0)
{
mergeCursor = new MergeCursor(new Cursor[]{mergeCursor, cursorFollowingFeed});
}else {
mergeCursor = new MergeCursor(new Cursor[]{cursorFeed, cursorFollowingFeed});
}
//This code is for merging the cursors.
if (intCtr + 1 == cursorFollowing.getCount()) {
cursorFollowing.close();
} else {
cursorFollowing.moveToNext();
}
}
ListViewAdapterMeasurement adapterMeasurement = new ListViewAdapterMeasurement(this, mergeCursor);
listviewFeed.setAdapter(adapterMeasurement);
}else
{
ListViewAdapterMeasurement adapterMeasurement = new ListViewAdapterMeasurement(this, cursorFeed);
listviewFeed.setAdapter(adapterMeasurement);
}
It is all working well. I just want to order the posts by Date or by ID.
Is there any way to sort MergeCursor?
I guess there is no way to sort the MergeCursor so I tried to think of other ways.
I changed my query like this:
public static Cursor getFeed (DBHelper dbHelper, String strUserID)
{
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor cursorFollowing = getFollowing(dbHelper,strUserID);
String strQuery = "SELECT * FROM feed_tbl WHERE "+ Tables.FeedTable.COLUMN_USERID + "="+strUserID;
if(cursorFollowing.getCount()>0)
{
for(int intCtr=0;intCtr<cursorFollowing.getCount();intCtr++)
{
int intUseridI = cursorFollowing.getColumnIndex(Tables.FollowTable.COLUMN_USERID);
String strUseridI = cursorFollowing.getString(intUseridI);
String strConcatQuery = " OR "+ Tables.FeedTable.COLUMN_USERID + "="+strUseridI;
if (intCtr + 1 == cursorFollowing.getCount()) {
cursorFollowing.close();
} else {
cursorFollowing.moveToNext();
}
strQuery = strQuery +""+strConcatQuery;
Log.v(TAG,strQuery);
}
}
Cursor cursor = db.rawQuery(strQuery+" ORDER BY "+ Tables.FeedTable.COLUMN_ID + " DESC",null);
if (cursor != null) {
cursor.moveToFirst();
}
return cursor;
}
The result on the log tag is this:
V/FeedMe: SELECT * FROM feed_tbl WHERE feed_userid=1 OR feed_userid=2 OR feed_userid=4 OR feed_userid=5
Just to make things clear to those who have the same problem with me which is about MergeCursor sorting. THERE IS NO SUCH WAY TO SORT MERGECURSOR :)
Thankyou!

SQLite date query returns no record

I am developing an Android application where I search for records which are created after a given date. My code is some thing like this
public List<NPRMember> IncrementalData(String LastDtTime) {
List<NPRMember> results = new ArrayList<NPRMember>();
SQLiteDatabase db=getMyReadableDatabase();
String lastdt="datetime("+LastDtTime+")";
Cursor cursor = null;
try{
cursor = db.query(TABLE_NAME_NPR, new String[] { KEY_FULLNAME, KEY_FATHERNAME,
KEY_RCRD_SOURCE,KEY_RCRD_CRN_DATE},KEY_RCRD_CRN_DATE + ">? AND "+KEY_RCRD_SOURCE+">?",
new String[]{lastdt, "0"}, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
NPRMember nprmem = cursorToMemberDetails(cursor);
results.add(nprmem);
cursor.moveToNext();
}
}catch(Exception e){
Log.e(APP_NAME, "An error occurred while searching for "+LastDtTime+": "+e.toString(), e);
}finally{
if(cursor!=null && !cursor.isClosed()){
cursor.close();
}
}
return results;
}
The query does not return any value, although I have at least one record with KEY_RCRD_CRN_DATE 2013-07-25 18:59:19
The LastDtTime passed as parameter has value 2013-07-25 14:46:03.
One interesting thing is if I run the query at SQLite command prompt it returns the deisred record.
SELECT fullname, fathername, .... rcrdsource, rcrdcrtndate FROM nprmembers WHERE rcrdcrtndate>'2013-07-25
14:46:03' AND rcrdsource>0;
Any help would be appreciated.
Thanks
The parameter value (in lastdt) is wrong.
What you want is the string 2013-07-25 18:59:19, but you are actually using the string datetime(2013-07-25 18:59:19).
The letter d comes after 2, so no record matches.
Change the initialization of lastdt to:
String lastdt=LastDtTime;

Why am I getting NullPointerException here?

I have this code in one of my activity's onCreate Method
GetNews newsReporter = new GetNews(getApplicationContext());
try{
News[] allNews = newsReporter.getAllNews();
Log.d("News Count", String.valueOf(allNews.length));
String[] timestamps = new String[allNews.length];
String[] texts = new String[allNews.length];
for(int i=0;i<allNews.length;i++)
{
// timestamps[i] = allNews[i].getNewsTime();
texts[i] = allNews[i].getNewsText();
// Log.d("TimeStamp", timestamps[i]);
Log.d("Text", texts[i]);
}
}catch(Exception e){
Log.e("Error News", e.toString());
}
News Count Displays 6 in Logcat, which means News[] is not null.
But I receive NullPointerException on Line texts[i] = allNews[i].getNewsTime();
this is my News Class
public class News {
private int id;
private String timestamp;
private String text;
public News(int i,String t, String ti)
{
this.id=i;
this.text = t;
this.timestamp = ti;
}
public String getNewsTime()
{
return this.timestamp;
}
public String getNewsText()
{
return this.text;
}
}
P.S. News is stored in a SQLitedatabase, when i pulled the database from my DDMS, it contains all 6 rows with valid values none of them is null.
Edit:
This is my GetAllNews Method
public News[] getAllNews(){
SQLiteDatabase db = ConMan.OpenDBConnection();
try{
Cursor cursor = db.query(News_Table, Columns, null, null, null, null, null);
if(cursor!=null)
{
cursor.moveToFirst();
}
News[] allNews = new News[cursor.getCount()];
int i =0;
while(cursor.isLast()){
allNews[i] = new News(Integer.parseInt(cursor.getString(0)),
cursor.getString(1),cursor.getString(2));
cursor.moveToNext();
i++;
}
db.close();
ConMan.close();
return allNews;
}catch(Exception e)
{
Log.e("News DB Errors", e.getMessage());
}
return null;
}
The problem is in the newsReporter.getAllNews() method. Looks like it is returning the array without the value initialized.
News[] allNews = newsReporter.getAllNews();
Meaning,
allNews.length might get you some value. But at each index, you are missing the value or at least one or more of the indexes are missing the value in the array.
Do the printing like below to see if you have values
for (News news : allNews)
System.out.println(news);
Looks like it is not going into the following block at all.
while(cursor.isLast()){
allNews[i] = new News(Integer.parseInt(cursor.getString(0)),
cursor.getString(1),cursor.getString(2));
cursor.moveToNext();
i++;
}
Check whether cursor.isLast() method is returning true to get into this loop.
texts[i] = allNews[i].getNewsText();
Most possibly, allNews[someIndex] is null. when called getnewsText() on null throws NPE.
best test is to output allNews[i] or check if it isnull.
System.out.println(allNews[i]==null)
An array of size 6 can have 6 null references. Print each of your News objects in allNews, I bet 10 Obamas that at least one position in the array is null.
You are saying that allNews[] is not null, so it must be that News[] contains a null, so that allNews[i].getNewsText() throws the exception.

Manipulating database output in Android strings

I'm trying to manipulate the output of an SQLite query so into two parts - KEY_DATE and KEY_ROWID + KEY_TYPE. Here's the code without the KEY_TYPE bit:
DataBaseAdapter db = new DataBaseAdapter(this);
db.open();
// get last test date
int whichItem = 1;
int upDown = 1; // use 1 for descending ordering
Cursor mCursor = db.getLogType(whichItem, upDown);
String[] columns = new String[] {DataBaseAdapter.KEY_DATE, DataBaseAdapter.KEY_ROWID};
startManagingCursor(mCursor);
ListAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_expandable_list_item_2, mCursor, columns, new int[] {android.R.id.text1, android.R.id.text2}){
public boolean areAllItemsEnabled(){
return false;
}
public boolean isEnabled(int position){
return false;
}
};
setListAdapter(adapter);
db.close();
It works fine, but when I replace:
String[] columns = new String[] {DataBaseAdapter.KEY_DATE, DataBaseAdapter.KEY_ROWID};
with:
String[] columns = new String[] {DataBaseAdapter.KEY_DATE, DataBaseAdapter.KEY_ROWID + DataBaseAdapter.KEY_TYPE};
it doesn't work.
All help much appreciated!
What you are doing in the:
String[] columns = new String[] {DataBaseAdapter.KEY_DATE, DataBaseAdapter.KEY_ROWID + DataBaseAdapter.KEY_TYPE}
You are concat strings DataBaseAdapter.KEY_ROWID + DataBaseAdapter.KEY_TYPE that will leads you to some new column name that is hybrid of both of them (e.g if KEY_ROWID="_id" and KEY_TYPE="device" then the new column name will be "_iddevice"). This might affect the result. Make sure you have column that has the same name as the concationation of these two column names I mentioned above. But if you want to include the KEY_TYPE in your result, add KEY_TYPE as separate element of the array that holds your column names. Then concat the result from both columns in result rows. (I'm assuming this is what you were trying to achieve)
String[] columns = new String[] {DataBaseAdapter.KEY_TYPE, DataBaseAdapter.KEY_ROWID};
List<MyResult> list = new ArrayList<MyResult>();
Cursor result_of_query = database.query(DataBaseAdapter.TABLE_NAME, columns, null, null, null, null, null);
if(result_of_query.moveToFirst())
{
while(result_of_query.isAfterLast()==false)
{
MyResult result = new MyResult();
result.setId(result_of_query.getInt(result_of_query.getColumnIndex(DataBaseAdapter.KEY_ROWID))));
result.setType(result_of_query.getString(result_of_query.getColumnIndex(DataBaseAdapter.KEY_TYPE))));
result_of_query.moveToNext();
list.add(result);
}
}
result_of_query.close();
---
public class MyResult
{
private String type;
private int id;
public void setType(String t)
{
type=t;
}
public String getType()
{
return type;
}
public void setId(int t)
{
id=t;
}
public int getId()
{
return id;
}
}
---
Okay so I found a simpler answer. The problem was really with the concatenation. I was trying to do it in String[] columns... but actually what I needed to do was to concatenate at as part of the SELECT query and use an alias. Hence I changed the String[] columns to include the column alias name KEY_ROWID_TYPE as follows:
String[] columns = new String[] {DataBaseAdapter.KEY_DATE, DataBaseAdapter.KEY_ROWID_TYPE};
And the query is then:
Cursor mCursor = db.query(true, DATABASE_LOGTABLE, new String[] {KEY_ROWID, KEY_TYPE, KEY_DATE, KEY_ROWID + " || \"-\" || " + KEY_TYPE + " as " + KEY_ROWID_TYPE}, null, null, null, null, null, null);
And of course I defined the KEY_ROWID_TYPE too.
Thanks to everyone for your help.
I would try concatenation and then insert into array
String tempStr = DataBaseAdapter.KEY_ROWID + DataBaseAdapter.KEY_TYPE;
String[] columns = new String[] {DataBaseAdapter.KEY_DATE,tempStr};

Categories