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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:25:52+00:00 2026-05-31T19:25:52+00:00

Update getting error when i try to call the method from Activity: SQLiteDatabase db

  • 0

Update

getting error when i try to call the method from Activity:

SQLiteDatabase db = null;
List<Album> r = dbHelper.getAllAlbum(db);  

in my dbHelper i hae a method called getAllAlbum

// Getting All Contacts
public List<Album> getAllAlbum(SQLiteDatabase db) {
List<Album> rec = new ArrayList<Album>();
// Select All Query
String selectQuery = "SELECT  * FROM " + KEY_TABLE;

Cursor cursor = db.rawQuery(selectQuery, null);

// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
    Album r = new Album();
    r.setID(Integer.parseInt(cursor.getString(0)));
    r.setAlbumID(cursor.getString(1));
      ...............
      ...............
    rec.add(r);
} while (cursor.moveToNext());
}
// return contact list
return rec;
}

03-25 20:12:48.995: E/AndroidRuntime(27297): FATAL EXCEPTION: main
03-25 20:12:48.995: E/AndroidRuntime(27297): java.lang.RuntimeException: Unable to start activity ComponentInfo{net.website.Media_Player_Activity}: java.lang.NullPointerException
03-25 20:12:48.995: E/AndroidRuntime(27297):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
03-25 20:12:48.995: E/AndroidRuntime(27297):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
03-25 20:12:48.995: E/AndroidRuntime(27297):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-25 20:12:48.995: E/AndroidRuntime(27297):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
03-25 20:12:48.995: E/AndroidRuntime(27297):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-25 20:12:48.995: E/AndroidRuntime(27297):    at android.os.Looper.loop(Looper.java:123)
03-25 20:12:48.995: E/AndroidRuntime(27297):    at android.app.ActivityThread.main(ActivityThread.java:3683)
03-25 20:12:48.995: E/AndroidRuntime(27297):    at java.lang.reflect.Method.invokeNative(Native Method)
03-25 20:12:48.995: E/AndroidRuntime(27297):    at java.lang.reflect.Method.invoke(Method.java:507)
03-25 20:12:48.995: E/AndroidRuntime(27297):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-25 20:12:48.995: E/AndroidRuntime(27297):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-25 20:12:48.995: E/AndroidRuntime(27297):    at dalvik.system.NativeStart.main(Native Method)
03-25 20:12:48.995: E/AndroidRuntime(27297): Caused by: java.lang.NullPointerException

END UPDATE

i have searched before i post the question here but does not fix my problem so end up posting my question…

i am trying to insert few rows in sqlhelper class and when i get the below error…

logcat:

03-25 05:34:06.725: E/AndroidRuntime(17465): java.lang.RuntimeException: Unable to start activity ComponentInfo{net.website.Media_Player_Activity}: java.lang.IllegalStateException: getWritableDatabase called recursively
03-25 05:34:06.725: E/AndroidRuntime(17465):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
03-25 05:34:06.725: E/AndroidRuntime(17465):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
03-25 05:34:06.725: E/AndroidRuntime(17465):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-25 05:34:06.725: E/AndroidRuntime(17465):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
03-25 05:34:06.725: E/AndroidRuntime(17465):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-25 05:34:06.725: E/AndroidRuntime(17465):    at android.os.Looper.loop(Looper.java:123)
03-25 05:34:06.725: E/AndroidRuntime(17465):    at android.app.ActivityThread.main(ActivityThread.java:3683)
03-25 05:34:06.725: E/AndroidRuntime(17465):    at java.lang.reflect.Method.invokeNative(Native Method)
03-25 05:34:06.725: E/AndroidRuntime(17465):    at java.lang.reflect.Method.invoke(Method.java:507)
03-25 05:34:06.725: E/AndroidRuntime(17465):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-25 05:34:06.725: E/AndroidRuntime(17465):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-25 05:34:06.725: E/AndroidRuntime(17465):    at dalvik.system.NativeStart.main(Native Method)
03-25 05:34:06.725: E/AndroidRuntime(17465): Caused by: java.lang.IllegalStateException: getWritableDatabase called recursively
03-25 05:34:06.725: E/AndroidRuntime(17465):    at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:101)
03-25 05:34:06.725: E/AndroidRuntime(17465):    at net.issoa.db.DatabaseHelper.getReciterCount1(DatabaseHelper.java:120)
03-25 05:34:06.725: E/AndroidRuntime(17465):    at net.issoa.db.DatabaseHelper.onCreate(DatabaseHelper.java:80)
03-25 05:34:06.725: E/AndroidRuntime(17465):    at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:126)
03-25 05:34:06.725: E/AndroidRuntime(17465):    at net.issoa.db.DatabaseHelper.getAllReciterObj(DatabaseHelper.java:144)
03-25 05:34:06.725: E/AndroidRuntime(17465):    at net.issoa.Media_Player_Activity.doInBackground(Media_Player_Activity.java:535)
03-25 05:34:06.725: E/AndroidRuntime(17465):    at net.issoa.Media_Player_Activity.onCreate(Media_Player_Activity.java:106)
03-25 05:34:06.725: E/AndroidRuntime(17465):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-25 05:34:06.725: E/AndroidRuntime(17465):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)



public class DatabaseHelper extends SQLiteOpenHelper {

public DatabaseHelper(Context context) {
        super(context, KEY_DB_NAME, null, KEY_DATABASE_VERSION); 
    }

@Override
    public void onCreate(SQLiteDatabase db) { 

        db.execSQL("CREATE TABLE " + KEY_TABLE + "(" + 
                KEY_ID + " INTEGER PRIMARY KEY, " +
                KEY_ALBUM_ID + " TEXT, " + 
                ");"); 
        //Inserts pre-defined departments 
        if (getAlbumCount() == 0)
        {
            InsertAlbum();
        }
    }

    int getAlbumCount()
    {
        SQLiteDatabase db=this.getReadableDatabase();
        Cursor cur= db.rawQuery("Select * from "+ KEY_TABLE, null);
        int x= cur.getCount();
        cur.close();
        return x;
    }

        void InsertAlbum()
    { 
         ContentValues cv = new ContentValues(); 
             cv.put(KEY_ALBUM_ID, KEY_ALBUM_ID_VALUE);
             ................
             SQLiteDatabase db = this.getWritableDatabase();
        db.insert(KEY_TABLE, null, cv);
        db.close();
        } 
}
  • 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-31T19:25:53+00:00Added an answer on May 31, 2026 at 7:25 pm

    What happens is that getReadableDatabase() calls onCreate() wich calls getAlbumCount wich calls getReadableDatabase() wich calls onCreate() … you can see where this is going.

    The easy fix is send db as a parameter to getAlbumCount() like this

    public class DatabaseHelper extends SQLiteOpenHelper {
    
    public DatabaseHelper(Context context) {
            super(context, KEY_DB_NAME, null, KEY_DATABASE_VERSION); 
        }
    
    @Override
        public void onCreate(SQLiteDatabase db) { 
    
            db.execSQL("CREATE TABLE " + KEY_TABLE + "(" + 
                    KEY_ID + " INTEGER PRIMARY KEY, " +
                    KEY_ALBUM_ID + " TEXT, " + 
                    ");"); 
            //Inserts pre-defined departments 
            if (getAlbumCount(db) == 0)
            {
                InsertAlbum(db);
            }
        }
    
        int getAlbumCount(SQLiteDatabase db)
        {
    
            Cursor cur= db.rawQuery("Select * from "+ KEY_TABLE, null);
            int x= cur.getCount();
            cur.close();
            return x;
        }
    
            void InsertAlbum(SQLiteDatabase db)
        { 
             ContentValues cv = new ContentValues(); 
                 cv.put(KEY_ALBUM_ID, KEY_ALBUM_ID_VALUE);
                 ................
    
            db.insert(KEY_TABLE, null, cv);
            db.close();
            } 
    }
    

    RESPONSE TO UPDATE: You are getting a null pointer exception beacause you are sending null to getAllAlbum().

    SQLiteDatabase db = dbHelper.getWritableDatabse(db);
    List<Album> r = dbHelper.getAllAlbum(db);  
    

    A much neater solution is to overload the getAllAlbum() method like this.

    public class DatabaseHelper extends SQLiteOpenHelper {
    
         public List<Album> getAllAlbum() {
         //never ever call this method from within onCreate()
         return getAllAblum(this.getWritableDatabase());
         }
    
         private List<Album> getAllAlbum(SQLiteDatabase db) {
         // get results here
         //only call this method from within the class
         }
    
    }
    

    You can aply this to the previous methods as well if you need both outside and inside access.
    This way you could just call getAllAlbum() with no parameters from the outside and avoid recursive calls to onCreate().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am getting the following error when trying to update my project from Intellij
I am getting this error when running a save insert/update command: SEVERE: null java.sql.SQLIntegrityConstraintViolationException:
I'm getting an error when I try to persist an entity from a class.
I am getting the following error when I try to update or add contents
I'm getting a 1064 error when trying to call a stored function from within
I am trying to do a terminal update and I keep getting this error,
I am trying to update code via Linq, but I am getting this error:
I am getting the following error when trying to update an object using nhibernate.
I am getting the following error after running git svn rebase: 'update-index --refresh: command
UPDATE: I tried implementing the method specified by Peter and am getting incorrect shadowing.

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.