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) {
}
});