2

I save config with SharedPreferences and load it in class extends Services.

But error appear when I try get values of config, error like:

Unable to instantiate service kr.co.composer.callrecord.recorder.CallRecordService: java.lang.NullPointerException

at line:

final SharedPreferences mSharedPreference= PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

In layout Configuration I save a EditText:

final SharedPreferences prefs = PreferenceManager
        .getDefaultSharedPreferences(this);

serverUrl.setText(prefs.getString("autoSave", ""));
serverUrl.addTextChangedListener(new TextWatcher() {
    @Override
    public void onTextChanged(CharSequence s, int start, int before,
                              int count)
    {
    }
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
                                  int after)
    {
    }
    @Override
    public void afterTextChanged(Editable s)
    {
        prefs.edit().putString("autoSave", s.toString()).commit();
    }
});

And in the other class, I want to get this values by this code:

public class CallRecordService extends Service {
    final SharedPreferences mSharedPreference= PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    @Override
    public void onCreate() {
        serverURL =(mSharedPreference.getString("autoSave", ""));
    }
}
Ave
  • 4,338
  • 4
  • 40
  • 67

2 Answers2

1

You can't call getApplicationContext() before onCreate(). Instead, you can do it like this:

public class CallRecordService extends Service {
    @Override
    public void onCreate() {
        final SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
        serverURL = sp.getString("autoSave", "");
    }
}
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
1

NullPointerException is thrown when an application attempts to use an object reference, having the null value.

Don't

  public class CallRecordService extends Service {
    final SharedPreferences mSharedPreference= PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    // Call this in your onCreate() section .
    @Override
    public void onCreate() {
        serverURL =(mSharedPreference.getString("autoSave", ""));
    }
}

Do

 public class CallRecordService extends Service {
    final SharedPreferences mSharedPreference= 
    @Override
    public void onCreate() {
      PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); // You can pass context here
        serverURL =(mSharedPreference.getString("autoSave", ""));
    }
}
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
  • 1
    @IntelliJ Just FYI: The OP is talking about a `Service`; you quoted the docs for `Activity`. Also, a `final` field can only be assigned in a constructor. If you meant to show the `mSharedPreference` initialization in `onCreate()`, you need to remove the `final` modifier in the declaration, and add `mSharedPreference =` in front of the first line in `onCreate()`. – Mike M. Apr 06 '16 at 06:38
  • @MikeM. Sir .Mine mistake .Thanks for your keen info. – IntelliJ Amiya Apr 06 '16 at 06:40