I'm trying to get the value stored in Shared Preference else return default value. Here is my code :
Long count = SharedPref.getSdkInfo(context, "count");
This line is in onCreate.
public static Long getSdkInfo(Context context,String key){
SharedPreferences sharedPref = context.getSharedPreferences(ApiConstant.SDK_INFO, 0);
Long data = sharedPref.getLong(key, 0L);
return data;
}
getSdkInfo method is in different class called SharedPref. Here key contains a string value which is also used while saving value in shared preference.
Here is the error :
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.viral.snapminttracker/com.viral.snapminttrackersdk.SendDataActivity}: 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 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
I have tried many examples but none worked and not able to identify mistake. Please help. Thanks
No, the link provided is just an explaination why nullPointerException occurs, I have posted my code, which seems to be correct but still getting the error. I'm asking if it has any mistake then please point it out