0

I am currently writing a function that allows the user to see his or her rating. The issue I have been having is that occasionally, the function will work perfectly fine while other times, the app crashes stating i have a null pointer exception. The issue is on my line of code

 txtStars = (TextView)findViewById(R.id.rating);
 txtStars.setText(Common.currentUser.getRates());

Specifically on the getRates() function. This function comes from my User class which is

public class User {

    private String email, password, name, phone,rates;
    public User (){}

    public User(String email, String password, String name, String phone, String rates){
        this.email = email;
        this.password = password;
        this.name = name;
        this.phone = phone;
        this.rates = rates;
    }

    public String getRates() {
        return rates;
    }

    public void setRates(String rates) {
        this.rates = rates;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getName() {
        return name;
    }

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

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }
}

For a little extra help, here is my logcat

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.usub/com.example.usub.DriverProfile}: java.lang.NullPointerException: Attempt to invoke virtual method 'int com.example.usub.Model.User.getRates()' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int com.example.usub.Model.User.getRates()' on a null object reference
        at com.example.usub.DriverProfile.onCreate(DriverProfile.java:56)
        at android.app.Activity.performCreate(Activity.java:7825)
        at android.app.Activity.performCreate(Activity.java:7814)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1306)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016) 
        at android.os.Handler.dispatchMessage(Handler.java:107) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:7356) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 

Thank you

EDIT: ANSWER What I noticed was that the value, currentUser was only established at the log in. Because of this, the data would soon be forgotten due to the value never being reset. The app has a "remember me" feature which would bypass the first initialization of currentUser. To fix this, I added the code below in every activity that needed currentUser

 FirebaseDatabase.getInstance().getReference(Common.user_driver_tbl)
                .child(FirebaseAuth.getInstance().getCurrentUser().getUid())
                .addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                        Common.currentUser = dataSnapshot.getValue(User.class);
                    }

                    @Override
                    public void onCancelled(@NonNull DatabaseError databaseError) {

                    }
                });
Stradtdog
  • 1,490
  • 2
  • 7
  • 13

2 Answers2

1

It appears that currentUser is a static field of the Common class. That is, you have code somewhere that looks something like this:

public class Common {

    public static User currentUser;

    //...
}

It is likely that you initialize currentUser at some point. However, if your app's process is killed (the user navigates away and the system wants to reclaim resources), that value will be lost. When your app's process is restarted, currentUser isn't automatically restored... and so it is null.

You will have to either (a) figure out a way to save currentUser across process death (perhaps you can make it part of some Activity's instance state and use onSaveInstanceState() to save it) or (b) figure out a way to re-initialize currentUser when resuming after process death.

Read more here: https://developer.android.com/topic/libraries/architecture/saving-states

Ben P.
  • 52,661
  • 6
  • 95
  • 123
1

currentUser object is null. Before getting the current user object make sure it is initialized correctly. This might happen when app in background and OS clear the object from the memory.try saving the state of that object and then use.

Dipankar Baghel
  • 1,932
  • 2
  • 12
  • 24