samedi 21 février 2015

Android:Copy database in assets folder null pointer exception

I have read almost all the topics related to my problem in here so please do not say it is duplicate or has been questioned before.


I created the tables and the database in Sqlite Expert Personal 3. I copied it in assets folder. Right now I want to tell the android to check if there isnt already a database, copy the database from assets folder. so I do this:



datasource = new DataSource(this);
List<Prayer> prayers = datasource.findAllPrayers();

if (prayers.size == 0)
{
dbopenhelper.copyDataBase();
}
datasource.open();


Here it should check if there is any row at the table "prayers" and if there wasn't any rows, it should copy the database. but when I run the program, I get nullPointerException.


Here is my findAllPrayers() method which is in datasource class:



public List<Prayer> findAllPrayers()
{

List<Prayer> prayerlist = new ArrayList<Prayer>();
//
Cursor cursor = database.query(DBOpenHelper.TABLE_PRAYER,
allPrayerColumns, null, null, null, null, null);
//
Log.i(LOGTAG, "Child Cursor returned " + cursor.getCount() + " row(s).");

if (cursor.getCount() > 0)
{

while (cursor.moveToNext())
{

Prayer p = new Prayer();
p.setPrayer_name(cursor.getString(cursor
.getColumnIndex(DBOpenHelper.COLUMN_PRAYER_NAME)));

p.setPrayer_ID(cursor.getLong(cursor
.getColumnIndex(DBOpenHelper.COLUMN_PRAYER_ID)));
p.setPrayer(cursor.getString(cursor
.getColumnIndex(DBOpenHelper.COLUMN_PRAYER)));

prayerlist.add(p);
}
}

return prayerlist;

}




And here is my OpenHelper class:



package org.bihe.database;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBOpenHelper extends SQLiteOpenHelper
{

public static final String LOGTAG = "PRAYERBOOK_APP";
//
private static Context context;
//
private static String DB_PATH;
//
public static final String DATABASE_NAME = "Prayers.db";
public static final int DATABASE_VERSION = 1;
// ***Tables***
public static final String TABLE_PRAYERTOPIC = "prayertopic";
public static final String TABLE_PRAYER = "prayer";
public static final String TABLE_FAVORITES = "favorites";
public static final String TABLE_PRAYEROFTOPIC = "prayeroftopic";
public static final String TABLE_FAVORITEPRAYERS = "favoriteprayers";
public static final String TABLE_AUDIOPRAYERS = "audioprayers";
// ***Table prayertopic columns***
public static final String COLUMN_PRAYERTOPIC_ID = "prayertopic_ID";
public static final String COLUMN_PRAYERTOPIC_NAME = "prayertopic_name";
// ***Table prayer columns***
public static final String COLUMN_PRAYER_ID = "prayer_ID";
public static final String COLUMN_PRAYER_NAME = "prayer_name";
public static final String COLUMN_PRAYER = "prayer";
// ***Table favorites columns***
public static final String COLUMN_FAVORITE_ID = "favorite_ID";
public static final String COLUMN_FAVORITE_NAME = "favorite_name";
// ***Table prayeroftopic columns***
public static final String COLUMN_PRAYEROFTOPIC_ID = "prayeroftopic_ID";
public static final String COLUMN_PRAYER_ID_POT = "prayer_ID";
public static final String COLUMN_PRAYERTOPIC_ID_POT = "prayertopic_ID";
// ***Table favoriteprayer columns***
public static final String COLUMN_FAVORITEPRAYER_ID = "favoriteprayer_ID";
public static final String COLUMN_FAVORITE_ID_FP = "favorite_ID";
public static final String COLUMN_PRAYER_ID_FP = "prayer_ID";
// ***Table auidioprayers***
public static final String COLUMN_AUDIOPRAYER_ID = "audioprayer_ID";
public static final String COLUMN_AUDIOPRAYER_NAME = "audioprayer_name";
public static final String COLUMN_AUDIOPRAYER_ADDRESS = "audioprayer_address";

// ***Creating Tables***
private static final String CREATE_PRAYER_TABLE = "CREATE TABLE "
+ TABLE_PRAYER + " (" + COLUMN_PRAYER_ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_PRAYER_NAME
+ " TEXT, " + COLUMN_PRAYER + " TEXT " + ")";
//
private static final String CREATE_PRAYERTOPIC_TABLE = "CREATE TABLE "
+ TABLE_PRAYERTOPIC + " (" + COLUMN_PRAYERTOPIC_ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_PRAYERTOPIC_NAME
+ " TEXT" + ")";
//
private static final String CREATE_PRAYEROFTOPIC_TABLE = "CREATE TABLE "
+ TABLE_PRAYEROFTOPIC + " (" + COLUMN_PRAYEROFTOPIC_ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + " FOREIGN KEY("
+ COLUMN_PRAYER_ID_POT + ") REFERENCES " + TABLE_PRAYER + "("
+ COLUMN_PRAYER_ID + ") ON UPDATE CASCADE ON DELETE CASCADE "
+ " FOREIGN KEY(" + COLUMN_PRAYERTOPIC_ID_POT + ") REFERENCES "
+ TABLE_PRAYERTOPIC + "(" + COLUMN_PRAYERTOPIC_ID
+ ") ON UPDATE CASCADE ON DELETE CASCADE " + ") ";

public DBOpenHelper(Context ctx)
{
super(ctx, DATABASE_NAME, null, DATABASE_VERSION);
context = ctx;
DB_PATH = "/data/data/" + context.getPackageName() + "/" + "databases/";
}

//
public static void copyDataBase()
{
try
{
InputStream input = context.getAssets().open(DATABASE_NAME);
FileOutputStream output = new FileOutputStream(DB_PATH
+ DATABASE_NAME);
byte[] data = new byte[1024];
int buffer;
while ((buffer = input.read(data)) != -1)
{
output.write(data, 0, buffer);
}
output.flush();
output.close();
input.close();
} catch (IOException ex)
{
Log.e(LOGTAG, "Error in copying database...");
}
}

//

// ***--***
@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL(CREATE_PRAYER_TABLE);
Log.i(LOGTAG, "Table has been created");

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
// db.execSQL("DROP TABLE IF EXISTS " + TABLE_PRAYER);
onCreate(db);

}

}


I would be very grateful if someone could tell me what is wrong with my code. THANKS IN ADVANCE


Aucun commentaire:

Enregistrer un commentaire