The app crashed when retrieving data from the database. What I want is when user navigates to profile, it will shows the data being registered in the database. This is what is shown by Android Studio in the RUN section when the app crashes:
E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.emergencynotificationhealthcare, PID: 24585
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String
java.lang.Object.toString()' on a null object reference
at com.example.emergencynotificationhealthcare.profileGuardian$1.onDataChange(profileGuardian.java:43)
at com.google.firebase.database.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:75)
at com.google.firebase.database.core.view.DataEvent.fire(DataEvent.java:63)
at com.google.firebase.database.core.view.EventRaiser$1.run(EventRaiser.java:55)
at android.os.Handler.handleCallback(Handler.java:761)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:6605)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:999)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:889)
This is the reported profileGuardian.java:
public class profileGuardian extends AppCompatActivity {
TextInputLayout guardian_username, guardian_email, guardian_phoneNo, guardian_password, address;
TextView usernameLabel, emailLabel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile_guardian);
//Hooks
guardian_username = (TextInputLayout)findViewById(R.id.full_name_profile);
guardian_email = (TextInputLayout)findViewById(R.id.email_profile);
guardian_phoneNo = (TextInputLayout)findViewById(R.id.phoneNo_profile);
guardian_password = (TextInputLayout)findViewById(R.id.password_profile);
emailLabel = (TextView)findViewById(R.id.email_field);
usernameLabel = (TextView)findViewById(R.id.username_field);
DatabaseReference rootNode = FirebaseDatabase.getInstance().getReference();
DatabaseReference reference = rootNode.child("user");
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
String username = dataSnapshot.child("username").getValue().toString();
String email = dataSnapshot.child("email").getValue().toString();
String password = dataSnapshot.child("password").getValue().toString();
String phoneNo = dataSnapshot.child("phoneNo").getValue().toString();
guardian_username.getEditText().setText(username);
guardian_email.getEditText().setText(email);
guardian_password.getEditText().setText(password);
guardian_phoneNo.getEditText().setText(phoneNo);
usernameLabel.setText(username);
emailLabel.setText(email);
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
throw error.toException(); // never ignore errors
}
});
}
}