Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8706179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:33:04+00:00 2026-06-13T03:33:04+00:00

I have an AsyncTask class inside my MapActivity. Inside the doInBacground() method I have

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T03:33:05+00:00Added an answer on June 13, 2026 at 3:33 am

    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;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code inside my asynctask: @Override protected void onPreExecute() { waitDialog =
I have created an asynchronous task like this: private class LongOperationcheckall extends AsyncTask<String, String,
I have code that starts a ProgressDialog inside an AsyncTask , it looks like
I have an arraylist of hashmaps inside the fragment activity class like this. This
I have the following asynctask class which is not inside the activity. In the
I have an AsyncTask with a textview loaded in the class so it looks
I have a class that extends AsyncTask. In the doInBackground() method, I connect to
I have an AsyncTask which calls my LocationHandler class method getLocation() which runs a
I have an Activity Class with an inner protected class that extends AsyncTask. I
I have class that extends AsyncTask class and I create instance of this class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.