-1

In my project I want to share my referral code with app link.I already save referral code in SharedPreferences but I am unable to set that message in a separate class.

public static SharedPreferences sharedPreferences;

public static void shareMyApp(Context context) {

    String link = "https://play.google.com/store/apps/details?id="+context.getPackageName()+"Signup with my referral code.My Code is "+sharedPreferences.getString("member_id",null);

    Intent sharingIntent = new Intent(Intent.ACTION_SEND);
    sharingIntent.setType("text/*");
    sharingIntent.putExtra(Intent.EXTRA_TEXT, link);
    context.startActivity(Intent.createChooser(sharingIntent, "Share Via"));
}

This method is present inside a java class.When this method called an error appear.

java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.String android.content.SharedPreferences.getString(java.lang.String, java.lang.String)' on a null object reference

How Can I print that code with app link?

Arpan Sarkar
  • 189
  • 3
  • 14
  • You don't initialize sharedPreferences, hence why it is null. https://developer.android.com/training/data-storage/shared-preferences#java – EDToaster Jun 06 '19 at 18:37

2 Answers2

1

Did you initialized sharedPreferences?

something like

sharedPreferences = getSharedPreferences("", Context.MODE_PRIVATE);
Djaf
  • 256
  • 1
  • 6
1
public static void shareMyApp(Context context) {
    SharedPreferences sharedPreferences = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
    String link = "https://play.google.com/store/apps/details?id="+context.getPackageName()+"Signup with my referral code.My Code is "+sharedPreferences.getString("member_id",null);

    Intent sharingIntent = new Intent(Intent.ACTION_SEND);
    sharingIntent.setType("text/plain");
    sharingIntent.putExtra(Intent.EXTRA_TEXT, link);
    context.startActivity(Intent.createChooser(sharingIntent, "Share Via"));
}

Here, I:

  • Move sharedPreferences to a local variable
  • Initialize it, using the approach you outlined in the comments (you will need to add a static import for PREF_NAME or otherwise fix that bit up to refer to your existing PREF_NAME constant)
  • Use text/plain instead of text/* (you are sending the content, so you specify what the MIME type is)
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • But get referral code as null – Arpan Sarkar Jun 06 '19 at 18:55
  • @ArpanSarkar: You may be using a different `SharedPreferences`, then. Somewhere else in your app, you are working with `SharedPreferences` to save data. There are lots of `SharedPreferences` objects, and I do not know what one you used. In my sample, I showed one common approach for getting a `SharedPreferences` object. As I wrote, you should replace `PreferenceManager.getDefaultSharedPreferences(context)` from my example with whatever code you are presently using to get your `SharedPreferences` object. – CommonsWare Jun 06 '19 at 18:57
  • what SharedPreferences is suitable for your answer – Arpan Sarkar Jun 06 '19 at 19:03
  • @ArpanSarkar: You are welcome to save your data to `PreferenceManager.getDefaultSharedPreferences(context)`. That is a common `SharedPreferences` for the entire app, and it is what preference screens use. – CommonsWare Jun 06 '19 at 19:04
  • public MyAppPrefsManager(Context context) { sharedPreferences = context.getSharedPreferences(PREF_NAME, PRIVATE_MODE); prefsEditor = sharedPreferences.edit(); } – Arpan Sarkar Jun 06 '19 at 19:07
  • I use this SharedPreferences custom – Arpan Sarkar Jun 06 '19 at 19:08
  • @ArpanSarkar: That is fine. Replace `PreferenceManager.getDefaultSharedPreferences(context)` in my sample with `context.getSharedPreferences(PREF_NAME, PRIVATE_MODE)` (though you will need to ensure that you can get access to the `PREF_NAME` constant). – CommonsWare Jun 06 '19 at 19:13
  • please edit your answer in code. – Arpan Sarkar Jun 06 '19 at 19:16
  • also get same result.get referral code as null – Arpan Sarkar Jun 06 '19 at 19:24
  • @ArpanSarkar: Then I have no means of helping you further. As I have stated repeatedly, you need to use the same code for reading the `SharedPreferences` as you do for writing the `SharedPreferences`. You will need to do that yourself. – CommonsWare Jun 06 '19 at 19:26
  • thanks for your response – Arpan Sarkar Jun 06 '19 at 19:34