-2

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

Sandip Singh
  • 386
  • 5
  • 16
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Max Vollmer Oct 23 '19 at 10:20
  • No, That is 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. – Sandip Singh Oct 23 '19 at 10:34
  • Don't know how we are supposed to find what is null in this specific context with 2 lines of code – Pablo Oct 23 '19 at 10:47
  • Ok, editing for adding more codes – Sandip Singh Oct 23 '19 at 11:02
  • 1
    Obviously `context` is null. We can't tell you why. The duplicate has all the info you need to fix this. – Max Vollmer Oct 23 '19 at 11:13

1 Answers1

0

Where does your "context" variable comes from? Be sure that it isn't null.

If, as you say, it is in the OnCreate method of your activity, you can change the line as follows:

Long count = SharedPref.getSdkInfo(this, "count");
josxha
  • 1,110
  • 8
  • 23