Reputation: 169
I am working on an android project that requires a database. The issue that I am having is when I go to start it up, i get a nullpointerexception error. Going through the logcat, I have narrowed it down, to when the database updating, and trying to open. Is this normal or am I over looking somthing?
logcat:
02-12 15:41:33.810: ERROR/AndroidRuntime(354): java.lang.RuntimeException: Unable to start activity ComponentInfo{arch.field/arch.field.ChooseSite}: java.lang.NullPointerException
02-12 15:41:33.810: ERROR/AndroidRuntime(354): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
02-12 15:41:33.810: ERROR/AndroidRuntime(354): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-12 15:41:33.810: ERROR/AndroidRuntime(354): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-12 15:41:33.810: ERROR/AndroidRuntime(354): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-12 15:41:33.810: ERROR/AndroidRuntime(354): at android.os.Handler.dispatchMessage(Handler.java:99)
02-12 15:41:33.810: ERROR/AndroidRuntime(354): at android.os.Looper.loop(Looper.java:123)
02-12 15:41:33.810: ERROR/AndroidRuntime(354): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-12 15:41:33.810: ERROR/AndroidRuntime(354): at java.lang.reflect.Method.invokeNative(Native Method)
02-12 15:41:33.810: ERROR/AndroidRuntime(354): at java.lang.reflect.Method.invoke(Method.java:507)
02-12 15:41:33.810: ERROR/AndroidRuntime(354): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-12 15:41:33.810: ERROR/AndroidRuntime(354): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-12 15:41:33.810: ERROR/AndroidRuntime(354): at dalvik.system.NativeStart.main(Native Method)
02-12 15:41:33.810: ERROR/AndroidRuntime(354): Caused by: java.lang.NullPointerException
02-12 15:41:33.810: ERROR/AndroidRuntime(354): at arch.field.BmDb$DatabaseHelper.onUpgrade(BmDb.java:96)
02-12 15:41:33.810: ERROR/AndroidRuntime(354): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:132)
02-12 15:41:33.810: ERROR/AndroidRuntime(354): at arch.field.BmDb.open(BmDb.java:110)
02-12 15:41:33.810: ERROR/AndroidRuntime(354): at arch.field.ChooseSite.onCreate(ChooseSite.java:30)
02-12 15:41:33.810: ERROR/AndroidRuntime(354): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-12 15:41:33.810: ERROR/AndroidRuntime(354): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-12 15:41:33.810: ERROR/AndroidRuntime(354): ... 11 more
OnUpgrade code:
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2)
{
Log.d("MonkeyDatabase","DbUpdater");
db.execSQL("Drop table if exists "+TABLE_SITE_NAME);
db.execSQL("Drop table if exists "+TABLE_UNIT_NAME);
db.execSQL("Drop table if exists "+TABLE_BONESHELL_NAME);
db.execSQL("Drop table if exists "+TABLE_CERAMIC_NAME);
db.execSQL("Drop table if exists "+TABLE_LITHIC_NAME);
onCreate(db);
}
Open code:
public BmDb open() throws SQLException
{
Log.d("Database","open");
db= BmDb.getWritableDatabase();
return this;
}
more code:
public BmDb(Context context)
{
this.context= context;
BmDb= new DatabaseHelper(context);
}
public static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context){
super(context, DATABASE_NAME, null, VERSION);
}
this is the code from the ChooseSite file(with line numbers):
final BmDb db=new BmDb(this);
db.open(); **this is the line mentioned in my logcat**
final Cursor c= db.GetSite();
if (c.moveToFirst()){
do{
sites(c);
}while (c.moveToNext());
}
any suggestions on what I am over looking?
Upvotes: 0
Views: 611
Reputation: 5095
In your onUpgrade
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2)
{
Log.d("MonkeyDatabase","DbUpdater");
db.execSQL("Drop table if exists "+TABLE_SITE_NAME);
db.execSQL("Drop table if exists "+TABLE_UNIT_NAME);
db.execSQL("Drop table if exists "+TABLE_BONESHELL_NAME);
db.execSQL("Drop table if exists "+TABLE_CERAMIC_NAME);
db.execSQL("Drop table if exists "+TABLE_LITHIC_NAME);
//onCreate(db);
onCreate(arg0);
}
onUpgrade()
is called, if the database version is increased in your application code. check your code ..
Upvotes: 0
Reputation: 1603
from this code
final BmDb db=new BmDb(this);
db.open(); **this is the line mentioned in my logcat**
final Cursor c= db.GetSite();
if (c.moveToFirst()){
do{
sites(c);
}while (c.moveToNext());
}
call this mehod twice db.open(); put this in try{}catch{} black.
as follows try{db.open();db.open();}catch(){}
Upvotes: 3