SQlite Database: Unable to Find Context

There seems to be some context issue in the next line when I get a NullPointerException . I think something is wrong with the context. Also, when I try to open and close a database of two actions, it throws a CannotOpenORCloseDatabase Error. Please, help.

 DBHelper dbHelper = new DBHelper(this.getApplicationContext()); 

Any idea why? Or it would be great if someone could offer a workaround.

 public static final String EXT_CATEGORY = "category"; public static final String EXT_URL = "url"; private String tableName = DBHelper.tableName; private SQLiteDatabase MyDb; private int category; Cursor c = null; public static final String EXT_POS = "position"; private String url; private int position; WebView mWebView; @Override protected void onCreate(Bundle savedInstanceState) { Log.v(LOG_TAG, "onCreate() called"); super.onCreate(savedInstanceState); openAndQueryDatabase(); } private void openAndQueryDatabase() { position = 1; category = 0; Log.v(LOG_TAG, "onCreate() called 3"); try { DBHelper dbHelper = DBHelper.getInstance(getBaseContext()); MyDb = dbHelper.getWritableDatabase(); c = MyDb.rawQuery("SELECT * FROM " + tableName + " where Category =" + category + "AND Position =" + position, null); Log.v(LOG_TAG, "onCreate() called 4"); // url = c.getString(c.getColumnIndex("Image")); url = "www.google.com"; Log.v(url, " URL in Recipe "); } catch (SQLiteException se) { Log.e(getClass().getSimpleName(), "Could not create or Open the database"); } 
+4
source share
4 answers

You must declare your DBHelper as a static instance (singleton) to ensure that only one DBHelper exists at any given time.

  public class DBHelper extends SQLiteOpenHelper { private static DBHelper mInstance = null; public static DBHelper getInstance(Context activityContext) { // Get the application context from the activityContext to prevent leak if (mInstance == null) { mInstance = new DBHelper (activityContext.getApplicationContext()); } return mInstance; } /** * Private, use the static method "DBHelper.getInstance(Context)" instead. */ private DBHelper (Context applicationContext) { super(applicationContext, DATABASE_NAME, null, DATABASE_VERSION); } } 

To open SQLiteDatabase, use:

 SQLiteDatabase mDataBase = DBHelper.getInstance(context).getWritableDatabase(); 

and close it

 mDataBase.close(); 
+3
source

try it

 db = new DbHelper(this.getContext()); 

It should work.

+2
source

You are using DBHelper dbHelper = new DBHelper(this.getApplicationContext());

You should use instead

 DBHelper dbHelper = new DBHelper(getApplicationContext()); 

Edit -

Try using

 MyDb = getApplicationContext().getWritableDatabase(); 

instead

 DBHelper dbHelper = new DBHelper(this.getApplicationContext()); MyDb = dbHelper.getWritableDatabase(); 

Also try removing the finally() block as suggested by this answer .

+1
source

use the constructor for this ... When calling the context of the DB class transfer as a parameter. as shown below.

  public class MyDB { private Context context; private DatabaseHelper DBHelper; private SQLiteDatabase db; public MyDB(Context ctx) { this.context = ctx; DBHelper = new DatabaseHelper(context); } } 

Hope this helps you.

+1
source

All Articles