I am having difficulty opening my database in my class. I am trying to get my application to start on the phone bootup, that is only if the boolean value in the database is true, if false it will not boot the application on startup. I have the errors to the bottom
public class My_BroadcastReceiver extends BroadcastReceiver {
SQLiteDatabase sqlDB;
private static String DBNAME2 = "database2.db";
private static String Table2 = "options";
Boolean isOk = true;
Context context;
@Override
public void onReceive(Context context, Intent intent) {
try {
sqlDB = SQLiteDatabase.openOrCreateDatabase(DBNAME2, null);
Cursor r = sqlDB.rawQuery("SELECT Apponstart FROM " + Table2, null);
System.out.println("COUNT of Location: " + r.getCount());
int loc = r.getColumnIndex("Apponstart");
int j = 0;
if (r.moveToFirst()) {
do {
System.out.println("Apponstart is " + r.getString(loc));
isOk = Boolean.parseBoolean(r.getString(loc));
j++;
} while (r.moveToNext());
if (j == 0) {
System.out.println("No data found");
}
}
r.close();
// sqlDB.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
sqlDB.close();
}
if(isOk){
Intent i = new Intent(context, JoshTwoActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
09-23 08:10:05.615: WARN/System.err(208): android.database.sqlite.SQLiteException: unable to open database file
09-23 08:10:05.625: WARN/System.err(208): at android.database.sqlite.SQLiteDatabase.dbopen(Native Method)
09-23 08:10:05.625: WARN/System.err(208): at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1812)
09-23 08:10:05.625: WARN/System.err(208): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:817)
09-23 08:10:05.625: WARN/System.err(208): at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:851)
09-23 08:10:05.625: WARN/System.err(208): at three.three.My_BroadcastReceiver.onReceive(My_BroadcastReceiver.java:31)
09-23 08:10:05.625: WARN/System.err(208): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2810)
09-23 08:10:05.625: WARN/System.err(208): at android.app.ActivityThread.access$3200(ActivityThread.java:125)
09-23 08:10:05.625: WARN/System.err(208): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2083)
09-23 08:10:05.625: WARN/System.err(208): at android.os.Handler.dispatchMessage(Handler.java:99)
09-23 08:10:05.625: WARN/System.err(208): at android.os.Looper.loop(Looper.java:123)
09-23 08:10:05.625: WARN/System.err(208): at android.app.ActivityThread.main(ActivityThread.java:4627)
09-23 08:10:05.625: WARN/System.err(208): at java.lang.reflect.Method.invokeNative(Native Method)
09-23 08:10:05.625: WARN/System.err(208): at java.lang.reflect.Method.invoke(Method.java:521)
09-23 08:10:05.625: WARN/System.err(208): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-23 08:10:05.625: WARN/System.err(208): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-23 08:10:05.625: WARN/System.err(208): at dalvik.system.NativeStart.main(Native Method)
And again at
09-23 08:10:06.046: ERROR/AndroidRuntime(208): FATAL EXCEPTION: main
09-23 08:10:06.046: ERROR/AndroidRuntime(208): java.lang.RuntimeException: Unable to start receiver three.three.My_BroadcastReceiver: java.lang.NullPointerException
09-23 08:10:06.046: ERROR/AndroidRuntime(208): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2821)
09-23 08:10:06.046: ERROR/AndroidRuntime(208): at android.app.ActivityThread.access$3200(ActivityThread.java:125)
09-23 08:10:06.046: ERROR/AndroidRuntime(208): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2083)
09-23 08:10:06.046: ERROR/AndroidRuntime(208): at android.os.Handler.dispatchMessage(Handler.java:99)
09-23 08:10:06.046: ERROR/AndroidRuntime(208): at android.os.Looper.loop(Looper.java:123)
09-23 08:10:06.046: ERROR/AndroidRuntime(208): at android.app.ActivityThread.main(ActivityThread.java:4627)
09-23 08:10:06.046: ERROR/AndroidRuntime(208): at java.lang.reflect.Method.invokeNative(Native Method)
09-23 08:10:06.046: ERROR/AndroidRuntime(208): at java.lang.reflect.Method.invoke(Method.java:521)
09-23 08:10:06.046: ERROR/AndroidRuntime(208): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-23 08:10:06.046: ERROR/AndroidRuntime(208): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-23 08:10:06.046: ERROR/AndroidRuntime(208): at dalvik.system.NativeStart.main(Native Method)
09-23 08:10:06.046: ERROR/AndroidRuntime(208): Caused by: java.lang.NullPointerException
09-23 08:10:06.046: ERROR/AndroidRuntime(208): at three.three.My_BroadcastReceiver.onReceive(My_BroadcastReceiver.java:57)
09-23 08:10:06.046: ERROR/AndroidRuntime(208): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2810)
09-23 08:10:06.046: ERROR/AndroidRuntime(208): ... 10 more
The database was the problem. I created a method and added context to the opening of the database. Here is my solution.