I’m using SharedPreferences in my application to store the last User id who logged in in my application,and I’m trying to do that from non-activity class, which cause me a NullPointerException in the second line of code :
Context mContext;
SharedPreferences lastUser = PreferenceManager.getDefaultSharedPreferences(mContext);
SharedPreferences.Editor editor = lastUser.edit();
I’ve tried with every possible variation of mContext :
Acticity.this.mContext;this.mContext.getApplicationContext();this.mContext;
but they didn’t solve my problem.
Can anybody give me some solution how to get rid of this problem and how to use Context in static way, without getting NullPointerException?
i think you had not initialize context of non Activity class, to get away through refer this Android saving data in Internal Storage NullPointerException