0

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

}
}

database structure

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Sally
  • 49
  • 5

1 Answers1

1

It is because you are adding value event listener on user node and user node contains all user like in the picture it contains John, Mary

and then they have fields like email, password....

so you have to change your code a little bit because the value event listener will give you all the users and then you can traverse every user to retrieve it fields like username etc and then check get the data of the user you want in the loop as below in code.

 for (DataSnapshot internalNode : dataSnapshot.getChildren()) {
        // Here it will run for all the users one by one

        String username = internalNode.child("username").getValue().toString();
        String email = internalNode.child("email").getValue().toString();
        String password = internalNode.child("password").getValue().toString();
        String phoneNo = internalNode.child("phoneNo").getValue().toString();

 }

However, if you want data of specific user like John then you can change database reference like and your own code will work fine then

DatabaseReference reference = rootNode.child("user/John");

Jalees Mukarram
  • 106
  • 1
  • 4