-1

What I'm trying to do:
I have the first login activity, where user login using Google account. Then I save user data to SheredPreferences:

private void handleSignInResult(GoogleSignInResult result) {
    if (result.isSuccess()) {
        GoogleSignInAccount acct = result.getSignInAccount();
        String userName = acct.getDisplayName();
        String userPhotoUrl = acct.getPhotoUrl().toString();

        String userEmail = acct.getEmail();

        UserPreference userPreference = new UserPreference(this);

        userPreference.setName(userName);
        userPreference.setEmail(userEmail);
        userPreference.setPhoto(userPhotoUrl);

        Intent mainIntent = new Intent(this, MainActivity.class);
        mainIntent.addFlags(
                Intent.FLAG_ACTIVITY_CLEAR_TOP).addFlags(
                Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        startActivity(mainIntent);
        finish();
    }
}

My preferences class:

public class UserPreference {

SharedPreferences prefs;

public UserPreference(Activity activity) {
    prefs = activity.getSharedPreferences("USER_PREFERENCES",Activity.MODE_PRIVATE); // error line 15
}

void setEmail(String email) {
    prefs.edit().putString("userEmail", email).apply();
}

String getEmail() {
    return prefs.getString("userEmail", "userEmail");
}

...

And my DBHelper class (I try to create individual db for each user):

public class DBTools extends SQLiteOpenHelper {

public DBTools(Context applicationContext, String name){

    super(applicationContext, name+"-database.db", null, 1);

}

...

But when I try to use it in my MainActivity:

DBTools mDBTools = new DBTools(this, new UserPreference(this).getEmail()); // error line 36 MainActivity

I get this error:

   Process: com.package, PID: 21667
 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.package/com.package.MainActivity}: 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:2334)
 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2483)
 at android.app.ActivityThread.access$900(ActivityThread.java:153)
 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1349)
 at android.os.Handler.dispatchMessage(Handler.java:102)
 at android.os.Looper.loop(Looper.java:148)
 at android.app.ActivityThread.main(ActivityThread.java:5441)
 at java.lang.reflect.Method.invoke(Native Method)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)
   Caused by: 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.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:181)
 at com.package.UserPreference.<init>(UserPreference.java:15)
 at com.package.MainActivity.<init>(MainActivity.java:36)
 at java.lang.Class.newInstance(Native Method)

I'll apreciate any help or hint

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
JohnA
  • 335
  • 1
  • 3
  • 12

1 Answers1

0

Your activity is a nullpointer, make sure you sent a valid context

AlexVestin
  • 2,548
  • 2
  • 16
  • 20