tufekoi
tufekoi

Reputation: 991

Using SQLite DB with fragment

So I have a basic activity with some tabs fragments. One of them open a connection to my db. The application start without any crash but when I rotate the phone and the view is recreated I get a "NullPointerException" on :

147    MySQLiteHelper dbHelper = new MySQLiteHelper(getActivity());
148    SQLiteDatabase db = dbHelper.getReadableDatabase();

debugger :

09-30 00:12:56.307    9690-9690/com.apps.jean.emergency E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
        at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
        at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:188)
        at com.apps.jean.emergency.Fragment.Call_Fragment.loadContinentDataInSpinner(Call_Fragment.java:148)

Can anyone help me with this ? Thanks !

Upvotes: 1

Views: 928

Answers (1)

Patrick Brennan
Patrick Brennan

Reputation: 2738

I would look at where you are making the call to new MySQLiteHelper(getActivity()). Is it in onCreate() ? Your Fragment may not be attached to an Activity yet. Try waiting until you're in the onAttach() method, then you'll have an Activity.

Upvotes: 2

Related Questions