iAziz iAziz - 12 days ago 5
Android Question

Logically true but 'if statement' with a method making my app crash

I have an AsyncTask class inside my MapActivity. Inside the doInBacground() method I have to blocks of codes.

It looks like this:

@Override
protected List<GeoPoint> doInBackground(String... arg0) {


// check if the query (word and the distance) existing queries and return its id
int checkQuery = db.getQueryMatch(arg0[3].toString() , arg0[2].toString());
Log.v(" CheckQuery int ", " equal? " + checkQuery );

if (checkQuery < 1)
{
Block 1
}
else
{
Block 2
}


getQueryMatch method will check if two text fields exist then return the record ID.

int getQueryMatch(String word, String meter) {
SQLiteDatabase db = this.getReadableDatabase();
int getidd = 0;
Cursor cursor = db.query(TABLE_HISTORY, new String[] { KEY_ID, QUERYWORD, DISTANCEQ, KEY_TIMESTAMP }, QUERYWORD + "=? AND " + DISTANCEQ + "=?", new String[] { word, meter }, null, null, null, null);
//String selectQuery = "SELECT * FROM " + TABLE_HISTORY + " WHERE " + QUERYWORD + "= '" + word + "' AND " + DISTANCEQ + "= '"+ meter +"'" ;
if (cursor != null)
cursor.moveToFirst();
getidd = Integer.parseInt(cursor.getString(0));

db.close(); // Closing database connection

// return History
return getidd;
}


When I tested it without the if statement in the AsyncTask class , it works just fine. It makes the app crashing if the returned value is 0.
So what I am trying to do is to have block 1 running if there is no records found from the DB (checked by getQueryMatch ) otherwise run block 2.

Here are the errors:

10-19 02:47:50.464: E/AndroidRuntime(11967): FATAL EXCEPTION: AsyncTask #2
10-19 02:47:50.464: E/AndroidRuntime(11967): java.lang.RuntimeException: An error occured while executing doInBackground()
10-19 02:47:50.464: E/AndroidRuntime(11967): at android.os.AsyncTask$3.done(AsyncTask.java:299)
10-19 02:47:50.464: E/AndroidRuntime(11967): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
10-19 02:47:50.464: E/AndroidRuntime(11967): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
10-19 02:47:50.464: E/AndroidRuntime(11967): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
10-19 02:47:50.464: E/AndroidRuntime(11967): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
10-19 02:47:50.464: E/AndroidRuntime(11967): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
10-19 02:47:50.464: E/AndroidRuntime(11967): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
10-19 02:47:50.464: E/AndroidRuntime(11967): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
10-19 02:47:50.464: E/AndroidRuntime(11967): at java.lang.Thread.run(Thread.java:856)
10-19 02:47:50.464: E/AndroidRuntime(11967): Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
10-19 02:47:50.464: E/AndroidRuntime(11967): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:418)
10-19 02:47:50.464: E/AndroidRuntime(11967): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
10-19 02:47:50.464: E/AndroidRuntime(11967): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
10-19 02:47:50.464: E/AndroidRuntime(11967): at com.example.phooogle.DatabaseHandler.getQueryMatch(DatabaseHandler.java:148)
10-19 02:47:50.464: E/AndroidRuntime(11967): at com.example.phooogle.GoogleMapsAppActivity$InitTask.doInBackground(GoogleMapsAppActivity.java:142)
10-19 02:47:50.464: E/AndroidRuntime(11967): at com.example.phooogle.GoogleMapsAppActivity$InitTask.doInBackground(GoogleMapsAppActivity.java:1)
10-19 02:47:50.464: E/AndroidRuntime(11967): at android.os.AsyncTask$2.call(AsyncTask.java:287)
10-19 02:47:50.464: E/AndroidRuntime(11967): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)


For me it's logically true but not working!

Answer

You need to check the value returned by movetofirst. if the cursor is empty it returns false

   boolean notEmpty = cursor.moveToFirst();
   if(notEmpty)
        getidd = Integer.parseInt(cursor.getString(0));

Edit:

getidd = -1;
 if (cursor != null){
   boolean notEmpty = cursor.moveToFirst();
   if(notEmpty)
        getidd = Integer.parseInt(cursor.getString(0));    
}
 db.close(); // Closing database connection
// return History
return getidd;