3

I'm having trouble accessing a SQLite DB in my Android application. I have created an application class that contains an instance of a SQLiteOpenHelper however whenever I try to open the DB in another activity, it crashes.

Here is my application class:

public class GlobalVariables extends Application {

    private LocationsData locations;

    public void onCreate(Context context) {
            locations = new LocationsData(context);
    }

    public LocationsData getLocations() {
           return locations;
    }
}

Here is my SQLiteOpenHelper class:

public class LocationsData extends SQLiteOpenHelper{
private static final String DATABASE_NAME = "locations.db";
private static final int DATABASE_VERSION = 1;

public static final String TABLE_LOCATIONS = "locations";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_NICKNAME = "nickname";

private static final String DATABASE_CREATE = "create table "
        + TABLE_LOCATIONS + "( " + COLUMN_ID
        + " integer primary key autoincrement, " + COLUMN_NICKNAME
        + " text not null);";

public LocationsData(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(DATABASE_CREATE);

}

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

}

}

And when I try to access the db's in another activity like this i get an error:

    @Override
    public void onResume()
    {
         locationsData=((GlobalVariables)getApplication()).getLocations();
         SQLiteDatabase db = locationsData.getReadableDatabase();
         super.onResume();
    }

Any help? Thank you!

Log Cat:
05-10 02:00:50.146: D/AndroidRuntime(532): Shutting down VM
05-10 02:00:50.146: W/dalvikvm(532): threadid=1: thread exiting with uncaught exception (group=0x40015560)
05-10 02:00:50.179: E/AndroidRuntime(532): FATAL EXCEPTION: main
05-10 02:00:50.179: E/AndroidRuntime(532): java.lang.RuntimeException: Unable to resume activity {com.John.seniorDesign/com.John.seniorDesign.LocationsTabActivity}: java.lang.NullPointerException
05-10 02:00:50.179: E/AndroidRuntime(532):  at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2120)
05-10 02:00:50.179: E/AndroidRuntime(532):  at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:138)
05-10 02:00:50.179: E/AndroidRuntime(532):  at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
05-10 02:00:50.179: E/AndroidRuntime(532):  at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:654)
05-10 02:00:50.179: E/AndroidRuntime(532):  at android.widget.TabHost.setCurrentTab(TabHost.java:326)
05-10 02:00:50.179: E/AndroidRuntime(532):  at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:132)
05-10 02:00:50.179: E/AndroidRuntime(532):  at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:456)
05-10 02:00:50.179: E/AndroidRuntime(532):  at android.view.View.performClick(View.java:2485)
05-10 02:00:50.179: E/AndroidRuntime(532):  at android.view.View$PerformClick.run(View.java:9080)
05-10 02:00:50.179: E/AndroidRuntime(532):  at android.os.Handler.handleCallback(Handler.java:587)
05-10 02:00:50.179: E/AndroidRuntime(532):  at android.os.Handler.dispatchMessage(Handler.java:92)
05-10 02:00:50.179: E/AndroidRuntime(532):  at android.os.Looper.loop(Looper.java:130)
05-10 02:00:50.179: E/AndroidRuntime(532):  at android.app.ActivityThread.main(ActivityThread.java:3683)
05-10 02:00:50.179: E/AndroidRuntime(532):  at java.lang.reflect.Method.invokeNative(Native Method)
05-10 02:00:50.179: E/AndroidRuntime(532):  at java.lang.reflect.Method.invoke(Method.java:507)
05-10 02:00:50.179: E/AndroidRuntime(532):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-10 02:00:50.179: E/AndroidRuntime(532):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-10 02:00:50.179: E/AndroidRuntime(532):  at dalvik.system.NativeStart.main(Native Method)
05-10 02:00:50.179: E/AndroidRuntime(532): Caused by: java.lang.NullPointerException
05-10 02:00:50.179: E/AndroidRuntime(532):  at com.John.seniorDesign.LocationsTabActivity.onResume(LocationsTabActivity.java:75)
05-10 02:00:50.179: E/AndroidRuntime(532):  at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1150)
05-10 02:00:50.179: E/AndroidRuntime(532):  at android.app.Activity.performResume(Activity.java:3832)
05-10 02:00:50.179: E/AndroidRuntime(532):  at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2110)
05-10 02:00:50.179: E/AndroidRuntime(532):  ... 17 more
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Seuby
  • 77
  • 1
  • 8
  • post the stack trace of the error from logcat. – superfell May 10 '12 at 01:57
  • I think it may have something to do with the context in onCreate() call in the GlobalVariables class not being right. But I am not sure... – Seuby May 10 '12 at 02:07
  • 1
    post the code for LocationsTabActivity.onResume – superfell May 10 '12 at 02:16
  • Its what the last part I wrote was, ill edit it though so its more clear – Seuby May 10 '12 at 02:20
  • Something is null at line 75: Caused by: java.lang.NullPointerException 05-10 02:00:50.179: E/AndroidRuntime(532): at com.John.seniorDesign.LocationsTabActivity.onResume(LocationsTabActivity.java:75) – theelfismike May 10 '12 at 02:46

3 Answers3

4

In GlobalVariables change context to getApplicationContext() in new LocationsData and remove Context from onCreate - this signature does not exist.

EDIT:

public class GlobalVariables extends Application {

    private LocationsData locations;

    @Override
    public void onCreate() {
            locations = new LocationsData(getApplicationContext());
    }

    public LocationsData getLocations() {
           return locations;
    }
}
Harald Wilhelm
  • 6,656
  • 11
  • 67
  • 85
0

Not sure what the cause is, but here's how I usually implement my db open/close functions:

In my db class:

public GroceryDB(Context ctx) {
    this.mCtx = ctx;
}

public GroceryDB open() throws SQLException {
    mDBHelper = new DBHelper(mCtx);
    mDb = mDBHelper.getWritableDatabase();
    return this;
}

public void close() {
    mDBHelper.close();
}

Then I call it in any other class like so:

    mDbHelper = new GroceryDB(getActivity());
    mDbHelper.open();

Hope this helps!

Barak
  • 16,318
  • 9
  • 52
  • 84
0

Over the course of your application's lifecycle, you always want to ensure that you are dealing with a single database instance. Check out this answer to a similar question. It gives a few examples of how to correctly open/close your database in your application.

Community
  • 1
  • 1
Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250