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 6317825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T15:34:35+00:00 2026-05-24T15:34:35+00:00

I have an SQLite query in my android app that seems to crash when

  • 0

I have an SQLite query in my android app that seems to crash when it takes too long to execute. It crashes with NullPointerException and tells me the line number…

When I put breakpoints around that line and see that it always gets filled with a variable, the app does not crash and does what it is supposed to.

So aside from having a phantom null pointer, it appears the problem is that the breakpoints actually slow things down giving the query time to complete. Without breakpoints it always crashes without fail.

Others here seem to have a similar problem, and I’ve read some things about SQLite taking an erratic amount of time to complete tasks, but this table should only ever have a few entries in it (the one I’m testing should only have three entries, 4 columns)

Suggestions on how to make it not crash? Perhaps put a thread wait inside the method that makes the query?

 public void fetchItemsToRemove() throws SQLException{
    Cursor mCursor =
            mapDb.query(myMain_TABLE, new String[] {myOtherId, myCustomID, myDATE}, null, null, null, null, null);

    if(mCursor.moveToFirst())
    {
            do
            {
                /*taking "dates" that were stored as plain text strings, and converting them to 
                *Date objects in a particular format for comparison*/

                String DateCompareOld = mCursor.getString(mCursor.getColumnIndex(myDATE));
                String DateCompareCurrent = "";
                Date newDate = new Date();
                DateCompareCurrent = newDate.toString();

                try {
                    DateCompareOld = (String)DateCompareOld.subSequence(0, 10);
                    DateCompareCurrent = (String)DateCompareCurrent.subSequence(0, 10);
                    SimpleDateFormat dateType =  new SimpleDateFormat("EEE MMM dd");
                    Date convertDate = dateType.parse(DateCompareOld);

                    newDate = dateType.parse(DateCompareCurrent);

                    if(convertDate.compareTo(newDate) < 0)
                    {
                        //remove unlim id
                        mapDb.delete(myMain_TABLE, myDATE + "=" + mCursor.getString(mCursor.getColumnIndex(myDATE)), null);

                    }

                } catch (ParseException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }while(mCursor.moveToNext());
            mCursor.close();
    }
    else
    {
        mCursor.close();    
    }


}

Now “line 342” where it crashes with NullPointerException is DateCompareOld = (String)DateCompareOld.subSequence(0, 10); where it gets a subsequence of the string. If it gets here and is null, this means the string was never filled at String DateCompareOld = mCursor.getString(mCursor.getColumnIndex(myDATE));

as if the query just got skipped because it took too long. Do note this is in a while loop, and I have done tests to make sure that the mCursor never goes out of bounds.

  • 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-05-24T15:34:35+00:00Added an answer on May 24, 2026 at 3:34 pm

    You’re deleting things from a DB table whilst iterating over the results of a query from that table. Sounds a bit dangerous.

    Try building a list, inside the loop, of things to be deleted, and then delete them in a single go after the loop finishes.

    Also, wrap the entire thing in a DB transaction. When you’re modifying the DB in a loop, that can make a huge difference to performance.

    EDIT: a quick explanation of transactions:

    A transaction allows you to combine a bunch of DB queries/modifications into a single atomic operation which either succeeds or fails. It’s primarily a safety mechanism so your DB isn’t stuck in an inconsistent state if something goes wrong half way through, but it also means that any modifications are committed to the DB’s file storage in a single shot rather than one at a time, which is much faster.

    You start the transaction at the start of your function:

    public void fetchItemsToRemove() throws SQLException{
        db.beginTransaction();
        Cursor mCursor = ....
    

    You set it as successful if the whole function completes without errors. This probably means you want to remove the inner try/catch and have an outer try/catch enclosing the loop. Then at the end of the try{ }, you can assume nothing’s gone wrong, so you call:

    db.setTransactionSuccessful(); 
    

    Then, in a finally clause, to make sure you always close the transaction whether it’s successful or otherwise:

    db.endTransaction();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have android app that talks to server and syncs some data int SQLite
I have an android app that is using a SQLite database to determine if
I have a pretty long sqlite query: const char *sql_query = SELECT statuses.word_id FROM
I have an activity that runs a query on a Sqlite DB, gets a
I'm working on my first android app and have an SQLite database set up
I am query my sqlite data base for my Android app. I then use
I have an SQLite query that, potentially, returns thousands of records. I need to
I'm developing a Android App. This App have an SQLite Database. This Database have
I'm developing an Android application that makes a Query using Activity.managedQuery() , which takes
I have some codes read the query results from sqlite: while (sqlite3_step(statement) == SQLITE_ROW)

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.