user2979816 user2979816 - 1 month ago 15
Android Question

Fatal exeption: main Runtime error

I get this in my logcat:

11-11 17:11:42.161: E/AndroidRuntime(23599): FATAL EXCEPTION: main
11-11 17:11:42.161: E/AndroidRuntime(23599): java.lang.NullPointerException
11-11 17:11:42.161: E/AndroidRuntime(23599): at com.example.lightalarmclock.AddBox.setAlarm(AddBox.java:164)
11-11 17:11:42.161: E/AndroidRuntime(23599): at com.example.lightalarmclock.AddBox.onOptionsItemSelected(AddBox.java:147)
11-11 17:11:42.161: E/AndroidRuntime(23599): at android.app.Activity.onMenuItemSelected(Activity.java:2642)
11-11 17:11:42.161: E/AndroidRuntime(23599): at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:1039)
11-11 17:11:42.161: E/AndroidRuntime(23599): at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
11-11 17:11:42.161: E/AndroidRuntime(23599): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
11-11 17:11:42.161: E/AndroidRuntime(23599): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
11-11 17:11:42.161: E/AndroidRuntime(23599): at com.android.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:547)
11-11 17:11:42.161: E/AndroidRuntime(23599): at com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:115)
11-11 17:11:42.161: E/AndroidRuntime(23599): at android.view.View.performClick(View.java:4247)
11-11 17:11:42.161: E/AndroidRuntime(23599): at android.view.View$PerformClick.run(View.java:17728)
11-11 17:11:42.161: E/AndroidRuntime(23599): at android.os.Handler.handleCallback(Handler.java:730)
11-11 17:11:42.161: E/AndroidRuntime(23599): at android.os.Handler.dispatchMessage(Handler.java:92)
11-11 17:11:42.161: E/AndroidRuntime(23599): at android.os.Looper.loop(Looper.java:137)
11-11 17:11:42.161: E/AndroidRuntime(23599): at android.app.ActivityThread.main(ActivityThread.java:5298)
11-11 17:11:42.161: E/AndroidRuntime(23599): at java.lang.reflect.Method.invokeNative(Native Method)
11-11 17:11:42.161: E/AndroidRuntime(23599): at java.lang.reflect.Method.invoke(Method.java:525)
11-11 17:11:42.161: E/AndroidRuntime(23599): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738)
11-11 17:11:42.161: E/AndroidRuntime(23599): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
11-11 17:11:42.161: E/AndroidRuntime(23599): at dalvik.system.NativeStart.main(Native Method)


So the error is showing a problem on 164th line, right?

So here is the code:

private void setAlarm (SQLiteDatabase db){

if (rep1 >= 0 || rep2 >= 0 || rep3 >= 0 || rep4 >= 0 || rep5 >= 0 || rep6 >= 0 || rep7 >= 0){
repB = 1;
} else { repB = 2; }
timeSet= pos4+pos3+":"+pos2+pos1;
db.execSQL("INSERT INTO TABLE alarmStack(alarm_time, alarm_repeat, rep1d, rep2d, rep3d, rep4d, rep5d, rep6d, rep7d) " +
"VALUES (timeSet, repB, rep1, rep2, rep3, rep4, rep5, rep6, rep7)");
}


Line 164 is the one with db.execSQL.

I guess I'm imputing values wrong but I can't seem to fix it so I'm coming here.
I guess the values in alarmStack(...) are viewed as variables but they should be a part of a string to indicate the columns I'm editing in SQL, but I cant find a solution or the right syntax for this.

EDIT:

So in the comments @user1676075 said that "db" var is null, to fix that I did this:

private static class dbHelper extends SQLiteOpenHelper{

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

}

@Override
public void onCreate(SQLiteDatabase db) {
//writing to database
if (rep1 >= 0 || rep2 >= 0 || rep3 >= 0 || rep4 >= 0 || rep5 >= 0 || rep6 >= 0 || rep7 >= 0){
repB = 1;
} else { repB = 0; }
//setting time
timeSet= pos4+pos3+":"+pos2+pos1;
//Writing
db.execSQL
("INSERT INTO TABLE alarmDB.alarmStack(alarm_time, alarm_repeat, rep1, rep2, rep3, rep4, rep5, rep6, rep7) " +
"VALUES (timeSet, repB, rep1, rep2, rep3, rep4, rep5, rep6, rep7)");

//launch backToMain
//backToMain(ourCtx);
}


But now for some reason it can't instantiate that dbHelper and I get this:

11-12 20:47:42.852: E/AndroidRuntime(18656): FATAL EXCEPTION: main
11-12 20:47:42.852: E/AndroidRuntime(18656): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.lightalarmclock/com.example.lightalarmclock.AddBox$dbHelper}: java.lang.InstantiationException: can't instantiate class com.example.lightalarmclock.AddBox$dbHelper; no empty constructor
11-12 20:47:42.852: E/AndroidRuntime(18656): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2266)
11-12 20:47:42.852: E/AndroidRuntime(18656): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2400)
11-12 20:47:42.852: E/AndroidRuntime(18656): at android.app.ActivityThread.access$600(ActivityThread.java:153)
11-12 20:47:42.852: E/AndroidRuntime(18656): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1269)
11-12 20:47:42.852: E/AndroidRuntime(18656): at android.os.Handler.dispatchMessage(Handler.java:99)
11-12 20:47:42.852: E/AndroidRuntime(18656): at android.os.Looper.loop(Looper.java:137)
11-12 20:47:42.852: E/AndroidRuntime(18656): at android.app.ActivityThread.main(ActivityThread.java:5298)
11-12 20:47:42.852: E/AndroidRuntime(18656): at java.lang.reflect.Method.invokeNative(Native Method)
11-12 20:47:42.852: E/AndroidRuntime(18656): at java.lang.reflect.Method.invoke(Method.java:525)
11-12 20:47:42.852: E/AndroidRuntime(18656): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738)
11-12 20:47:42.852: E/AndroidRuntime(18656): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
11-12 20:47:42.852: E/AndroidRuntime(18656): at dalvik.system.NativeStart.main(Native Method)
11-12 20:47:42.852: E/AndroidRuntime(18656): Caused by: java.lang.InstantiationException: can't instantiate class com.example.lightalarmclock.AddBox$dbHelper; no empty constructor
11-12 20:47:42.852: E/AndroidRuntime(18656): at java.lang.Class.newInstanceImpl(Native Method)
11-12 20:47:42.852: E/AndroidRuntime(18656): at java.lang.Class.newInstance(Class.java:1130)
11-12 20:47:42.852: E/AndroidRuntime(18656): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
11-12 20:47:42.852: E/AndroidRuntime(18656): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
11-12 20:47:42.852: E/AndroidRuntime(18656): ... 11 more

Answer

I got similar issue once and I had to add a default constructor with a super call to fix it. You can try it once.

     public dbHelper() {
       super();
     }
Comments