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

0

When I log in to the application, I save your data in SharedPreferences using GSON (to save the API response with id, name, email, etc.).

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

However, putting the application into production, I got some crashes:

> 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 that the user exists (since he entered the search screen and not the login screen), however for some reason the Id is null. I'm not sure and would like help finding 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.

    
asked by anonymous 25.08.2016 / 20:43

0 answers