Reputation: 113
I am implementing a cache for my android app and using SQLite Database to store server responses. The class for handling the DB is defined below. Also it has a SQLiteOpenHelper class which is used as a database helper.
The class CacheDB is called from public static CacheDB cacheDB = new CacheDB(context);
and I am also calling the method cacheDB.fetchDatafromDB("songs");
The code for the classes is given below.
package com.songs.lookup;
import java.util.ArrayList;
import java.util.List;
import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
public class CacheDB {
public CacheDB(Context context){
System.out.println("Before constructing ");
this.context = context;
this.dbHelper = new CacheDBHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
System.out.println("After constructing ");
}
private Context context;
private CacheDBHelper dbHelper;
private static final String DATABASE_NAME = "";
private static final int DATABASE_VERSION = 1;
private static final String song_TABLE_NAME = "songs";
private static final String tune_TABLE_NAME = "tunes";
private static final String person_TABLE_NAME = "persons";
private static final String COLUMN_NAME = "name";
private static final String song_TABLE_CREATE =
"CREATE TABLE " + song_TABLE_NAME + " (" +
COLUMN_NAME + " TEXT);";
private static final String tune_TABLE_CREATE =
"CREATE TABLE " + tune_TABLE_NAME + " (" +
COLUMN_NAME + " TEXT);";
private static final String person_TABLE_CREATE =
"CREATE TABLE " + person_TABLE_NAME + " (" +
COLUMN_NAME + " TEXT);";
class CacheDBHelper extends SQLiteOpenHelper{
public CacheDBHelper(Context context, String name, CursorFactory factory,
int version) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// System.out.println("Before the cachedbhelper");
System.out.println("After the cachedbhelper");
}
@Override
public void onCreate(SQLiteDatabase db) {
System.out.println("Here inside the oncreate of cacheDBHelper");
db.execSQL(song_TABLE_CREATE);
db.execSQL(tune_TABLE_CREATE);
db.execSQL(person_TABLE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
@SuppressLint("NewApi")
public void performOperation(String Operation, String table, ArrayList<String> array1)
{
SQLiteDatabase db = dbHelper.getWritableDatabase();
String INSERT = "insert into "
+ table + " (" + COLUMN_NAME + ") values (?)";
String DELETE = "delete from " + table;
String FETCH = "select DISTINCT(" + COLUMN_NAME + "from " + table + ")";
db.beginTransaction();
SQLiteStatement dbStmt = db.compileStatement(Operation == "INSERT" ? INSERT : DELETE);
if(Operation == "INSERT")
{
int aSize = array1.size();
for (int i = 0; i < aSize; i++) {
dbStmt.bindString(1, array1.get(i));
dbStmt.executeInsert();
}
}
if(Operation == "DELETE")
{
dbStmt.executeUpdateDelete();
}
if(Operation == "SELECT")
{
fetchDatafromDB(table);
}
db.setTransactionSuccessful();
db.endTransaction();
try {
db.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public List<String> fetchDatafromDB(String table) {
CacheDBHelper dbHelper = new CacheDBHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
SQLiteDatabase db = dbHelper.getWritableDatabase();
List<String> list = new ArrayList<String>();
// Select All Query
String selectQuery = "SELECT * FROM " + table;
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
cursor.getString(0);
} while (cursor.moveToNext());
}
// return contact list
return list;
}
}
I am getting the following stack trace
04-21 00:55:15.188: E/AndroidRuntime(790): Caused by: java.lang.NullPointerException
04-21 00:55:15.188: E/AndroidRuntime(790): at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
04-21 00:55:15.188: E/AndroidRuntime(790): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
04-21 00:55:15.188: E/AndroidRuntime(790): at com.songs.lookup.CacheDB.fetchDatafromDB(CacheDB.java:124)
04-21 00:55:15.188: E/AndroidRuntime(790): at com.songs.lookup.LookUpData.getData(LookUpData.java:25)
04-21 00:55:15.188: E/AndroidRuntime(790): at com.songs.MainActivity2.onCreate(MainActivity2.java:64)
04-21 00:55:15.188: E/AndroidRuntime(790): at android.app.Activity.performCreate(Activity.java:5008)
04-21 00:55:15.188: E/AndroidRuntime(790): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
04-21 00:55:15.188: E/AndroidRuntime(790): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
Where is the problem here?
Upvotes: 2
Views: 6331
Reputation: 1
The Context you passed to the SQLiteOpenHelper
constructor is null.
Upvotes: 0
Reputation: 471
I had a similar problem earlier and mine was because I was trying to open a database within a method that had already opened it earlier. I don't know if that makes sense, but I can see here where you call the dbHelper.getWritableDatabase()
method twice without closing the database after the first time.
I think that could be the cause of your issue, as the database is locked by the previous method, hence the error
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
Not too sure if I'm a 100% correct here, but hope this helps. cheers
Upvotes: 6