0

I tried to create custom class to fetch some values from SharedPreferences. My aim is to reach to that values from any class.

I am getting null Pointer exception on

 SharedPreferences prefs = getApplicationContext().getSharedPreferences("UserFile", MODE_PRIVATE);

My code is as below;

public class UserInfo extends Application {

    private String token;
    private String SAVED_USERNAME;

    public UserInfo() {
        SharedPreferences prefs = getApplicationContext().getSharedPreferences("UserFile", MODE_PRIVATE);
        token = prefs.getString("Token", null);
    }

    public String getToken() {
        return token;
    }
}

What might be the wrong?

frogatto
  • 28,539
  • 11
  • 83
  • 129
erdemgc
  • 1,701
  • 3
  • 23
  • 43
  • I guess when you getApplicationContext the app didn't start up at all ,so you can't get the context ,may be you should try getSharedpreference after the oncreate() method. – cowboi-peng Sep 22 '15 at 08:24
  • have a look at http://stackoverflow.com/questions/5018545/getapplication-vs-getapplicationcontext first answer – Shine Sep 22 '15 at 08:25

6 Answers6

1

Usually Android components are initialized during their lifecycle. In this particular case you can't access application Context and SharedPreferences because they're not initialized yet.

Second problem might be (thanks to my crystall ball) that you did not added your Application to AndroidManifest

So, your first thought might be to move initialization code from constructor to onCreate. This would solve this particular problem.

However, it's a bad practice to do what you're doing. Because there can be only 1 Application component per application. This will limit you to 1 such singleton per app. Consider using Application to provide application Context as singleton and create another singleton for providing UserInfo.

No examples, please exercise yourself.

Dmitry Zaytsev
  • 23,650
  • 14
  • 92
  • 146
1

Just have this method in a util class. No need to extend application.

public static String getToken(Context context) {
   return PreferenceManager.getDefaultSharedPreferences(context).getString("Token", null);
}
JoeyJubb
  • 2,341
  • 1
  • 12
  • 19
0

There is a rule in android - don't use constructor of app component: Activity/Fragment/Application/Service... there is onCreate() method, because in your constructor context will be null. So move your code to onCreate(). Also you need set your UserInfo as application in Manifest.

Viktor Yakunin
  • 2,927
  • 3
  • 24
  • 39
0

Make sure you have registered this class in your AndroidManifest.XML file.

<application android:name=".UserInfo"
    ...
/>

Note: Your way for accessing shared preferences does not seem good. I rather myself declare a class named PreferencesHelper and put all preferences stuff there.

public class PreferencesHelper{
    private SharedPreferences mPrefs;

    public PreferencesHelper(Context context){
        this.mPrefs = context.getSharedPreferences("name", Context.MODE_PRIVATE);
    }

    public getToken() {
        return mPrefs.getString("Token", null);
    }

    public String setToken(String token) {
        mPrefs.edit().putString("Token", token).apply();
    }
}
frogatto
  • 28,539
  • 11
  • 83
  • 129
0

You don't create constructor of Application class instead, use the code in onCreate():

 @Override
 public void onCreate() {
    super.onCreate();
    SharedPreferences prefs = getApplicationContext().getSharedPreferences("UserFile", MODE_PRIVATE);
    token = prefs.getString("Token", null);
 }

and use it from any activity:

UserInfo userInfo = (UserInfo)getApplication();
String token  = userInfo.getToken();
djhs16
  • 471
  • 1
  • 5
  • 12
  • I am not able to call getApplication() @djhs16 – erdemgc Sep 22 '15 at 08:31
  • @erdemgc you can call getApplication() only in activity. or if you want to use the data in other classes than activity then you can create static singleton class, no need to extend application. – djhs16 Sep 22 '15 at 08:48
0
public class MyApp extends Application {


 private static MyApp _instance;

@Override
public void onCreate() {
    super.onCreate();
   _instance = this;
}
public static MyApp getInstance(){
   return _instance;
}

public String getToken() {
    return getSharedPreferences("UserFile", MODE_PRIVATE).getString("Token",  null);
}
}

In your manifest:

<application
    android:name="your.package.MyApp"
   >

If you whant use :

String token = MyApp.getInstance().getToken();
Adam Miśtal
  • 715
  • 4
  • 15