Save a list with SharedPreferences on AndroidStudio

I am coding an app and struggling with saving a list with SharedPreferences.It announced that my app stopped working when i was running my app. Please help !! These are my codes and my logcat:

SharedPreferences SPre1 = getSharedPreferences(Context.MODE_PRIVATE);
Set<String> WorkListSet = SPre1.getStringSet("NOTES_SET", new HashSet<String>());
List<String> WorkList = new ArrayList<String>(WorkListSet);

public void Update1()
{
    SharedPreferences.Editor Editor = SPre1.edit();
    Set<String> WorkListSet = new HashSet<String>(WorkList);
    Editor.putStringSet("NOTES_SET", WorkListSet);
    Editor.apply();
}

private SharedPreferences getSharedPreferences(int modePrivate) {
    return null;
}

Stacktrace

Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'java.util.Set android.content.SharedPreferences.getStringSet(java.lang.String, java.util.Set)' on a null object reference
at com.newthingtank.along.hello.MainActivity.<init>(MainActivity.java:38)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.Class.newInstance(Class.java:1606)
at android.app.Instrumentation.newActivity(Instrumentation.java:1066)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2226)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
at android.app.ActivityThread.access$800(ActivityThread.java:151) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5254) 
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:903) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)  07

Upvotes: 0

Views: 85

Answers (2)

ramandeep singh
ramandeep singh

Reputation: 51

Saving Array in SharedPreferences

public static boolean saveArray() {

    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);

    SharedPreferences.Editor mEdit1 = sp.edit();


    mEdit1.putInt("Status_size", sKey.size());  

    for(int i=0;i<sKey.size();i++)  
    {
        mEdit1.remove("Status_" + i);
        mEdit1.putString("Status_" + i, sKey.get(i));  
    }

    return mEdit1.commit();     
}

Loading Array Data from SharedPreferences

public static void loadArray(Context mContext) {  




 SharedPreferences mSharedPreference1 =   PreferenceManager.getDefaultSharedPreferences(mContext);

    sKey.clear();

    int size = mSharedPreference1.getInt("Status_size", 0);  

    for(int i=0;i<size;i++) 
    {
     sKey.add(mSharedPreference1.getString("Status_" + i, null));
    }

}

Upvotes: 1

WarrenFaith
WarrenFaith

Reputation: 57672

private SharedPreferences getSharedPreferences(int modePrivate) {
    return null;
}

You are calling that method and you just return null. So you actually never get the shared prefs in the first place.

You need a context reference and call getSharedPreferences() on it. You have not provided enough information to say how you can get a reference to your context but any Activity instance would be enough as a context.

Upvotes: 0

Related Questions