-4

I'm using below code for shared preferences.

    private Context mCtx;   
private static SharedPrefManager mInstance;
    public SharedPrefManager(Context context) {
            mCtx = context;
        }    
public static synchronized SharedPrefManager getInstance(Context context) {
        if (mInstance == null)
            mInstance = new SharedPrefManager(context);
        return mInstance;
    }
    void SaveNotificationType(String strNotificationType) {
            SharedPreferences sharedPreferences = mCtx.getSharedPreferences(Shared_Pref_Name, Context.MODE_PRIVATE);
            Log.d(TAG, "SaveNotificationType: shared preferences is" + sharedPreferences);
            try {
                if(sharedPreferences != null) {
                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    editor.putString(notificationType, strNotificationType);
                    editor.apply();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            //editor.commit();
        }  

When I run above code, I get an error which is given below:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.SharedPreferences android.content.Context.getSharedPreferences(java.lang.String, int)' on a null object reference
        at com.example.durga_bikkina.storageunitburnstatus.SharedPrefManager.SaveNotificationType(SharedPrefManager.java:41)
        at com.example.durga_bikkina.storageunitburnstatus.MyFirebaseMessagingService.storeReceivedMessage(MyFirebaseMessagingService.java:125)
        at com.example.durga_bikkina.storageunitburnstatus.MyFirebaseMessagingService.onMessageReceived(MyFirebaseMessagingService.java:36)
        at com.google.firebase.messaging.FirebaseMessagingService.handleIntent(Unknown Source:340)
        at com.google.firebase.iid.zzg.run(Unknown Source:29)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
        at java.lang.Thread.run(Thread.java:764)  

How do I define context here?

Ajay Kulkarni
  • 2,900
  • 13
  • 48
  • 97

1 Answers1

1

Your stacktrace:

method Context.getSharedPreferences on a null object reference

So your mCtx is null. You need to provide non-null Context, application context also suitable.

E.g. you can provide it as a parameter, maybe from your Activity:

class MyActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // other actions like setContentView
        new SharedPrefManager(this).SaveNotificationType("Some text");

        // or you can also use applicationContext
        new SharedPrefManager(getApplicationContext()).SaveNotificationType("Some text");

        // or use singleton instance if you want
        SharedPrefManager.getInstance(this).SaveNotificationType("Some text");
    }
}
Peter Samokhin
  • 864
  • 1
  • 9
  • 19