I have a database helper class (code below).
This helper’s class task is to copy the database from my assets folder shipped with the app into the data\data… of my app so that i can use it.
Once I get the database into data\data (which i am able to).. I want to add on it and perform the CRUD operations, and this database shall remain in the app untill user removes the app.
However, Once the copying is done and I have the database in data\data.. I try to create an instance of DatabaseHelper in a different activity (the one that i want to add data from)..
and when I do so,,, I am getting a null pointer exception error the moment the code reaches
DatabaseHelper dbh = new DatabaseHelper(this);
I know possible causes for this can be wrong context, or a null database… but all seem to be okay in my case.
Below are the codes.
DatabaseHelper.java
public class DatabaseHelper extends SQLiteOpenHelper
{
String DB_PATH = null;
// assign the database givens, such as name and context.
private static String DB_NAME = "offline";
private SQLiteDatabase myDataBase;
private final Context myContext;
static int count = 0;
/**
* Constructor Takes and keeps a reference of the passed context in order to
* access to the application assets and resources.
*
* @param context
*/
public DatabaseHelper(Context context)
{
super(context, DB_NAME, null, 1);
this.myContext = context;
DB_PATH = "/data/data/" + context.getPackageName() + "/" + "databases/";
}
// ///////////////////////////////////////////////////////////////////////////////////////
public void createDataBase() throws IOException
{
boolean dbExist = checkDataBase();
if (dbExist)
{
// do nothing - database already exist
} else
{
this.getWritableDatabase();
try
{
copyDataBase();
} catch (IOException e)
{
throw new Error("Error copying database");
}
}
}
// ///////////////////////////////////////////////////////////////////////////////////////
private boolean checkDataBase()
{
SQLiteDatabase checkDB = null;
try
{
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null,SQLiteDatabase.OPEN_READWRITE);
} catch (SQLiteException e)
{
// database does't exist yet.
}
if (checkDB != null)
{
checkDB.close();
}
return checkDB != null ? true : false;
}
// ///////////////////////////////////////////////////////////////////////////////////////
private void copyDataBase() throws IOException
{
// Open your local db as the input stream
InputStream myInput = myContext.getAssets().open(DB_NAME);
// Path to the just created empty db
String outFileName = DB_PATH + DB_NAME;
// Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
// transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0)
{
myOutput.write(buffer, 0, length);
}
// Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
// ///////////////////////////////////////////////////////////////////////////////////////
public void openDataBase() throws SQLException
{
// Open the database
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null,SQLiteDatabase.OPEN_READWRITE);
}
// ///////////////////////////////////////////////////////////////////////////////////////
@Override
public synchronized void close()
{
if (myDataBase != null)
myDataBase.close();
super.close();
}
// return cursor
public Cursor query(String table, String[] columns, String selection,
String[] selectionArgs, String groupBy, String having,
String orderBy)
{
return myDataBase.query("myTable", null, null, null, null, null, null);
}
// //////////////////////////////////////////////////////////////////////////////////
@Override
public void onCreate(SQLiteDatabase db)
{
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
}
// //////////////////////////////////////////////////////////////////////////////////
public long insertPuzzle(String q,String a,String c1,String c2,String c3,String c4)
{
ContentValues initialValues = new ContentValues();
initialValues.put("q", q);
initialValues.put("a",a);
initialValues.put("c1",c1);
initialValues.put("c2",c2);
initialValues.put("c3",c3);
initialValues.put("c4",c4);
return myDataBase.insert("myTable", null, initialValues);
}
Whenever I do in my activity
DatabaseHelper db1 = new DatabaseHelper(this);
db1.openDatabase();
db1.insertPuzzle("q","a","1","2","3","4");
I get the null pointer error.
Hope you can be of help,
Regards,
put this file in your package
Activity onCreate method
and for insert code