When trying to get the user id saved in sharedPreferences, sometimes it returns null

Asked

Viewed 77 times

0

When logging into the app, I save your data to Sharedpreferences using GSON (to save the API response with id, name, email, etc).

The problem is that when starting the app, I check if the Model User is null. If not, it opens the search screen and from there I use User.getId() in several parts of the application.

However, when putting the app into production, I received some crashs:

> EXCEPTION java.lang.NullPointerException LOCATION VagasCalls.java line
> 180 in VagasCalls.getRecommendationIds() TIME Today, 4:35 MESSAGE
> Attempt to invoke virtual method 'int
> br.com.apps.jaya.vagas.Models.User.getId()' on a null object reference

I believe the user exists (since entered the search screen and not the login), but for some reason the Id is null. I’m not sure and would like help to discover the problem.

Note: This is only happening with some people.

Saving the user

public static void saveUser(User user) {

    Gson gson = new Gson();
    String json = gson.toJson(user);

    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putString(Constants.USER_INFO, json);
    editor.apply();
}

Getting the user

public static User getUser() {

    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
    Gson gson = new Gson();
    String json = sharedPreferences.getString(Constants.USER_INFO, "");
    return gson.fromJson(json, User.class);
}

Model - User

public class User implements Parcelable {

    @Expose
    @SerializedName("id")
    private int id;

    @SerializedName("identidade")
    @Expose
    private String identity;

    @SerializedName("nome")
    @Expose
    private String name;

    @SerializedName("foto")
    @Expose
    private String picture;

    @SerializedName("genero")
    @Expose
    private String genre;

    @Expose
    private String email;

    protected User(Parcel in) {
        id = in.readInt();
        identity = in.readString();
        name = in.readString();
        picture = in.readString();
        genre = in.readString();
        email = in.readString();
    }

    public static final Creator<User> CREATOR = new Creator<User>() {
        @Override
        public User createFromParcel(Parcel in) {
            return new User(in);
        }

        @Override
        public User[] newArray(int size) {
            return new User[size];
        }
    };

    public String getPicture() {
        return picture;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getIdentity() {
        return identity;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(id);
        dest.writeString(identity);
        dest.writeString(name);
        dest.writeString(picture);
        dest.writeString(genre);
        dest.writeString(email);
    }
}

Crash

> br.com.apps.jaya.vagas.VagasAPI   VagasCalls.java line 180 in
> VagasCalls.getRecommendationIds()
> br.com.apps.jaya.vagas.Helpers    NotificationsHelper.java line 143 in
> NotificationsHelper.getRecommendationIdsForUser()
> br.com.apps.jaya.vagas.Fragments  SearchFragment.java line 263 in
> SearchFragment.updateNotificationsIcon()
> br.com.apps.jaya.vagas.Fragments  SearchFragment.java line 179 in
> SearchFragment.onResume() android.app Fragment.java line 2096 in
> Fragment.performResume() android.app  FragmentManager.java line 928 in
> FragmentManagerImpl.moveToState() android.app FragmentManager.java
> line 1067 in FragmentManagerImpl.moveToState()
> android.app   FragmentManager.java line 1049 in
> FragmentManagerImpl.moveToState() android.app FragmentManager.java
> line 1879 in FragmentManagerImpl.dispatchResume()
> android.app   Activity.java line 6089 in Activity.performResume()
> android.app   ActivityThread.java line 3145 in
> ActivityThread.performResumeActivity() android.app    ActivityThread.java
> line 3187 in ActivityThread.handleResumeActivity()
> android.app   ActivityThread.java line 1411 in
> ActivityThread$H.handleMessage() android.os   Handler.java line 102 in
> Handler.dispatchMessage() android.os  Looper.java line 135 in
> Looper.loop() android.app ActivityThread.java line 5608 in
> ActivityThread.main() java.lang.reflect   Method.java line -2 in
> Method.invoke() java.lang.reflect Method.java line 372 in
> Method.invoke() com.android.internal.os   ZygoteInit.java line 1397 in
> ZygoteInit$MethodAndArgsCaller.run()
> com.android.internal.os   ZygoteInit.java line 1192 in ZygoteInit.main()

I’ve never been able to reproduce this problem. I’m using New Relic.

  • At what point in the application you call the Saveuser?

  • When I log in or register. I call the saveUser method by passing the API response - Accountmanager.saveUser(Response.body());

  • Did you completely uninstall the app before installing the production file? Maybe you saved the preferences as dev and ended up using to pro.

  • So, when you said moment. I meant, is the application taking time for that, is it not closing, changing screen or something. Got it? Never saved, or sometimes it works?

  • Most of the time it works, only happened with some people. Is there the possibility that the user is not null but fails in the parameters? If yes, as I treat?

  • @Cleidimarviana =/

  • @To find out if the user is logged in, I check if the user is null, but n check if the id is null. Will when saving it saves gson in Shared preferences but for some reason failed to save the parameters?

  • You checked if it’s happening for Android 6.0 as it might have something to do with tbm permission since it’s happening to some people.

  • @Cleidimarviana yes, I checked. It’s happening in the following versions: 4.4.2, 5.0.2, 6.0, 4.4.4

  • For these failed conditions try reading right after you have asked to save just to see what happens.

  • @Reginaldorigo I will try!! Thank you.

  • @user2391117 Take a look at this question: http://stackoverflow.com/questions/10786172/android-getdefaultsharedpreferences

Show 7 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.