0

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?

2 Answers 2

3

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(){}

Sign up to request clarification or add additional context in comments.

3 Comments

why would that help correct it? It worked, but just through up another error, and that one would be with my GetSite code, which is: public Cursor GetSite() { return db.query(TABLE_SITE_NAME, new String[]{KEY_SITEID, KEY_SITENAME, KEY_SITELOC}, null, null, null, null, null); } I am new to using SQLite with android, but from all the tutorials I have found, this is the common form. Did I mark it wrong?
looking at the android developer site this I found two instances of a cursor query. One with 8 parameters the other with 7. I am using the 7 paramater one, which is as follows: public Cursor query (SQLiteDatabase db, String[] projectionIn, String selection, String[] selectionArgs, String groupBy, String having, String sortOrder) As far as I can tell, I am following it correctly. Is there somthing that I am missing?
Ok, I am feeling stupid. Why does a try{}catch(){} statement make all the difference? That also fixed my other problem that I just commented on. I also found, that I only need to call the db.open() once in the try{}catch{}.
0

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 ..

1 Comment

the code to upgrade, is the marked in the actual program or would it be in my database file at the top; which I call VERSION?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.