2

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();
        } 
}
Nick Kahn
  • 19,652
  • 91
  • 275
  • 406
  • Your first two lines of code should be: SQLiteDatabase db = DatabaseHelper.getWritableDatabase(); List r = dbHelper.getAllAlbum(db); – bughi Mar 26 '12 at 18:32

2 Answers2

4

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().

bughi
  • 1,838
  • 1
  • 13
  • 24
0

Since you are using this in your onCreate and you are not passing in any parameters, you may want to make a private method to do this, and pass in the SQLiteDatabase parameter, so you always use the same database variable.

Since you are using this.getWriteableDatabase() I expect that it may point to the same database property and the fact that you call it more than once is probably your problem. I think this is a design problem which is why I suggested the fix I did.

UPDATE:

To fix the NPE just use this:

SQLiteDatabase db = this.getReadableDatabase();;
List<Album> r = dbHelper.getAllAlbum(db);  
James Black
  • 41,583
  • 10
  • 86
  • 166