-2

I'm trying to access SharedPreferences in a service.

public class LocationService extends Service implements 
    GoogleApiClient.ConnectionCallbacks, 
    GoogleApiClient.OnConnectionFailedListener, 
    LocationListener {

    Context context = getApplicationContext();
    SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context);

    ...

But I get this error:

E/AndroidRuntime: FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to instantiate service us.keithirwin.myapp.LocationService: java.lang.NullPointerException
        ...
    Caused by: java.lang.NullPointerException
        at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:101)
        at us.keithirwin.myapp.LocationService.<init>(LocationService.java:31)
        ...

LocationService.java:31 refers to Context context = getApplicationContext();.

I used to use this as context, i.e

PreferenceManager.getDefaultSharedPreferences(this);

That line started coming up null also. My research led me to use getApplicationContext() as my context. But it's nulls every way I turn.

LocationService.<init> sounds like the constructor. Other posts seem to say that I can't call preferences there. However, if I try to create sharedPref in onCreate, it's only available in that method. Is there some way to have the preferences available throughout the service?

ki9
  • 5,183
  • 5
  • 37
  • 48
  • 3
    override onCreate and move it into it – Blackbelt Dec 20 '15 at 16:58
  • I'd like to know why this question was downvoted. According to [the faq](http://stackoverflow.com/help/on-topic): `if your motivation is “I would like others to explain ______ to me”, then you are probably OK.` – ki9 Jul 12 '16 at 04:09

4 Answers4

3

LocationService.<init> sounds like the constructor.

Actually, it's an initializer for a data member.

However, if I try to create sharedPref in onCreate, it's only available in that method.

You are welcome to declare the data member outside of onCreate(). You cannot initialize the data member until inside onCreate():

public class LocationService extends Service {
  private SharedPreferences sharedPref;

  public void onCreate() {
    super.onCreate();

    sharedPref=PreferenceManager.getDefaultSharedPreferences(this);
  }

  // other cool stuff here
}
Mike M.
  • 38,532
  • 8
  • 99
  • 95
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
1

Try replacing

Context ctx = getApplicationContext();

with

Context ctx = this;
Mohammad Tauqir
  • 1,817
  • 1
  • 18
  • 53
1

ok, it's because this class (Service in this question) has not instantiated, so "this" is null. Also, I found a interesting question just like yours Can "this" ever be null in Java?. It might help

I think you should do this line: sharedPref=PreferenceManager.getDefaultSharedPreferences(this); after onCreate() as CommonWare suggested.

Community
  • 1
  • 1
shanwu
  • 1,493
  • 6
  • 35
  • 45
0

Service is Context. Service extends ContextWrapper and ContextWrapper extends Context therefore all you have to do is :

Context context = this;
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);

Hope it helps!!!

Kostas Drak
  • 3,222
  • 6
  • 28
  • 60