0

Hello i am learning android for some time, i want to make an app with more activities and one is a reminder, i have managed to clean the code of errors but when i run the app on phone and select that activity , it crashes, here is the log:

10-12 16:54:12.485 5364-5364/com.mihai_bucur.happycar E/AndroidRuntime:     FATAL EXCEPTION: main
                                                                    Process: com.mihai_bucur.happycar, PID: 5364
                                                                    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mihai_bucur.happycar/com.mihai_bucur.happycar.Documente}: java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.String android.content.SharedPreferences.getString(java.lang.String, java.lang.String)' on a null object reference
                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2702)
                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2767)
                                                                        at android.app.ActivityThread.access$900(ActivityThread.java:177)
                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1449)
                                                                        at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                        at android.os.Looper.loop(Looper.java:145)
                                                                        at android.app.ActivityThread.main(ActivityThread.java:5951)
                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                        at java.lang.reflect.Method.invoke(Method.java:372)
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
                                                                     Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.String android.content.SharedPreferences.getString(java.lang.String, java.lang.String)' on a null object reference
                                                                        at com.mihai_bucur.happycar.RemindMe.getDateRange(RemindMe.java:51)
                                                                        at com.mihai_bucur.happycar.Documente.onCreate(Documente.java:69)
                                                                        at android.app.Activity.performCreate(Activity.java:6289)
                                                                        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2655)
                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2767) 
                                                                        at android.app.ActivityThread.access$900(ActivityThread.java:177) 
                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1449) 
                                                                        at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                        at android.os.Looper.loop(Looper.java:145) 
                                                                        at android.app.ActivityThread.main(ActivityThread.java:5951) 
                                                                        at java.lang.reflect.Method.invoke(Native Method) 
                                                                        at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400) 
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195) 

Also here is the activity that is related to crash:

RemindMe.java

package com.mihai_bucur.happycar;

import android.app.Application;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.preference.PreferenceManager;
import com.mihai_bucur.happycar.model.DbHelper; 
/**
 * Created by Mihai on 03.10.2016.
 */
public class RemindMe extends Application {

//  private static final String TAG = "RemindMe";

public static DbHelper dbHelper;
public static SQLiteDatabase db;
public static SharedPreferences sp;

public static final String TIME_OPTION = "time_option";
public static final String DATE_RANGE = "date_range";
public static final String DATE_FORMAT = "date_format";
public static final String TIME_FORMAT = "time_format";
public static final String VIBRATE_PREF = "vibrate_pref";
public static final String RINGTONE_PREF = "ringtone_pref";

public static final String DEFAULT_DATE_FORMAT = "yyyy-M-d";


@Override
public void onCreate() {
    super.onCreate();


    PreferenceManager.setDefaultValues(this, R.xml.setari, false);
    sp = PreferenceManager.getDefaultSharedPreferences(this);

    dbHelper = new DbHelper(this);
    db = dbHelper.getWritableDatabase();
}


    public static boolean showRemainingTime() {
    return "1".equals(sp.getString(TIME_OPTION, "0"));
}

public static int getDateRange() {
    return Integer.parseInt(sp.getString(DATE_RANGE, "0"));
}

public static String getDateFormat() {
    return sp.getString(DATE_FORMAT, DEFAULT_DATE_FORMAT);
}

public static boolean is24Hours() {
    return sp.getBoolean(TIME_FORMAT, true);
}

public static boolean isVibrate() {
    return sp.getBoolean(VIBRATE_PREF, true);
}

public static String getRingtone() {
    return sp.getString(RINGTONE_PREF, android.provider.Settings.System.DEFAULT_NOTIFICATION_URI.toString());
}

}

at the getDateRage line the error appears

and also appears on my main activity at on create method at "int r = RemindMe.getDateRange();"

Documente.java

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_documente);
    findViews();
    db = RemindMe.db;
    /*font = Typeface.createFromAsset(getAssets(), "fonts/OpenSans-Semibold.ttf");*/
    headingText.setTypeface(font);
    monthArr = getResources().getStringArray(R.array.spinner3_arr);
    int r = RemindMe.getDateRange();

can you help me to solve this ? :D, i have searched on the internet but i was not able to find a solution and make it work Thanks!

  • I think you forgot to add your application class `RemindMe` in your `AndroidManifest.xml` file under application tag. `` – Kalpesh Patel Oct 12 '16 at 14:12
  • You cannot call it in a static way, as your `sp` parameter is not initialized. What you need to do, is call your application method using its instance. Or change your static methods to load `sp` – IAmGroot Oct 12 '16 at 14:13
  • Kalpesh Patel I added application class in android manifest file and still doesn't works. Doomsknight can you help me a little but more ti figure out how to call the application method? :D thanks – Bucur Mihai Oct 13 '16 at 00:02

0 Answers0