shrmaj
shrmaj

Reputation: 79

android - Can't access db - context always null

I am new to android and just getting started with my first app. Any help is greatly appreciated. I created a dbhelper class but it fails with a NullPointer exception. When I try to debug, the context always appears to be null, as a result of which the mDatabase is null too.

Following is my Code.

public class PharmaExpDBHelper {

    static final String DB_NAME = "pharmaApp.db";
    static final int VERSION = 1;
    private SQLiteDatabase db;
    private Context context;

    public PharmaExpDBHelper(Context context) {
        this.context = context;
        OpenHelper openHelper = new OpenHelper(this.context);
        this.db = openHelper.getWritableDatabase(); 
    }


    private static class OpenHelper extends SQLiteOpenHelper {

        OpenHelper(Context context) {
            super(context, DB_NAME, null, VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase database) {
            database.execSQL("CREATE TABLE pharmacy_list(id integer primary key autoincrement, name text);");
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            Log.w(PharmaExpDBHelper.class.getName(),
                    "Upgrading database from version " + oldVersion + " to "
                            + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS" + " pharmacy_list");
            onCreate(db);
        }


    }

}

Upvotes: 1

Views: 674

Answers (3)

chinna
chinna

Reputation: 31

public class PharmaExpDBHelper {

static final String DB_NAME = "pharmaApp.db"; 
static final int VERSION = 1; 
private SQLiteDatabase db; 

private PharmaExpDBHelper(Context context) {  
        super(context, DB_NAME, null, VERSION);  
    }  
 @Override    
    public void onCreate(SQLiteDatabase database) {    
        database.execSQL("CREATE TABLE pharmacy_list(id integer primary key autoincrement, name text);");    
    }    

    @Override    
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {    
        Log.w(PharmaExpDBHelper.class.getName(),    
                "Upgrading database from version " + oldVersion + " to "    
                        + newVersion + ", which will destroy all old data");    
        db.execSQL("DROP TABLE IF EXISTS" + " pharmacy_list");    
        onCreate(db);    
    }    

    public PharmaExpDBHelper open(){
              db=PharamExpDBHelper.getWriteableDatabase();
          return db;
     }
}

Now access this database in your Activity by

PharmaExpDBHelper dbhHelper=new PharmaExpDBHelper(this);
db.open();

try this code

Upvotes: 0

Tofeeq Ahmad
Tofeeq Ahmad

Reputation: 11975

in place of this code

public PharmaExpDBHelper(Context context) {
    this.context = context;
    OpenHelper openHelper = new OpenHelper(this.context);
    this.db = openHelper.getWritableDatabase(); 
}

use one method that return SQliteDatabase db

 public SQLiteDatabase getDBHelper(Context context) {
    this.context = context;
    OpenHelper openHelper = new OpenHelper(this.context);
    this.db = openHelper.getWritableDatabase(); 
    return this.db
}

Now access it in your activity

 SQLiteDatabase db=PharmaExpDBHelper class Obejct.getDBHelper(context);

Upvotes: 0

Argyle
Argyle

Reputation: 3394

I don't see anything obviously wrong with the posted code. Since you say the Context is null, you should make sure that the place where you instantiate this class is passing a valid Context instance.

It might help to throw an IllegalArgumentException if somebody passes in a null Context as a sanity check.

Upvotes: 1

Related Questions