NotPat
NotPat

Reputation: 23

error with Nullpointer exeption string in datasnapshot

I know this question has been asked in the past. I have tried almost all of their solutions did not worked for me. Sorry for asking this again. But can someone help me fix this error.

Here is my code.

public class ProfileActivity extends AppCompatActivity {

TextView userEmail;
TextView userID;
TextView userGender;
TextView userName;
Button userLogout;

FirebaseAuth firebaseAuth;
FirebaseUser firebaseUser;

FirebaseDatabase firebaseDatabase;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_profile);

    userEmail = findViewById(R.id.tvUserEmail);
    userGender = findViewById(R.id.tvGender);
    userName = findViewById(R.id.tvName);
    userID = findViewById(R.id.tvUserID);
    userLogout = findViewById(R.id.btnLogout);

    firebaseAuth = FirebaseAuth.getInstance();
    firebaseUser = firebaseAuth.getCurrentUser();


    firebaseDatabase = FirebaseDatabase.getInstance();
    DatabaseReference databaseReference = firebaseDatabase.getReference().child(firebaseUser.getUid());

    //userEmail.setText(firebaseUser.getEmail());

    databaseReference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

            String userEmailstr = dataSnapshot.child("email").getValue().toString();
            String userNamestr = dataSnapshot.child("name").getValue().toString();
            String userGenderstr = dataSnapshot.child("gender").getValue().toString();
            String userIDstr = dataSnapshot.child("id").getValue().toString();


            userEmail.setText(userEmailstr);
            userGender.setText(userGenderstr);
            userName.setText(userNamestr);
            userID.setText(userIDstr);
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
            Toast.makeText(getApplicationContext(),databaseError.getMessage(),Toast.LENGTH_SHORT).show();
        }
    });



    userLogout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            FirebaseAuth.getInstance().signOut();
            Intent intent = new Intent(ProfileActivity.this, MainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
            startActivity(intent);
        }
    });
}}

The error is on the Datasnapshot Strings. where it is said it is on a null object reference

Here is the logcat

 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
    at com.example.patrick.test.ProfileActivity$1.onDataChange(ProfileActivity.java:60)
    at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@16.0.5:75)
    at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@16.0.5:63)
    at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@16.0.5:55)

Here is my database enter image description here

Upvotes: 0

Views: 326

Answers (3)

Alex Mamo
Alex Mamo

Reputation: 138834

Whe you are using:

.child(firebaseUser.getUid())

It means that you are passing to the child method the id of the user that is coming from the Firebase authentication process. But passing this id will not help since in your database I see that you are having an id that is generated by the push() method and not the id from the FirebaseUser object.

So to solve this, you either pass to the child() method the correct id which is -LS3X ... 6Meg or you change the way in which you are adding the user to database by removing the push() call and use something like this:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
rootRef.child("Accounts").child(uid).setValue(userObject);

The second solution is the recommended one. Please also don't forget to add the Accounts extra child that is missing right now from your reference.

Upvotes: 2

Lekr0
Lekr0

Reputation: 733

You need to change

DatabaseReference databaseReference = firebaseDatabase.getReference().child(firebaseUser.getUid())

To

DatabaseReference databaseReference = firebaseDatabase.getReference().child("Accounts").child(firebaseUser.getUid());

And instead of

String userEmailstr = dataSnapshot.child("email").getValue().toString();

Use

String userEmailstr = dataSnapshot.child("email").getValue(String.class);

Upvotes: 2

Ali Ahmed
Ali Ahmed

Reputation: 2178

You need to change

DatabaseReference databaseReference = firebaseDatabase.getReference().child(firebaseUser.getUid());

To

DatabaseReference databaseReference = firebaseDatabase.getReference().child("Accounts").child(firebaseUser.getUid());

You're pointing to wrong node. that doesn't contains required data.

Note:

You don't need to store push_id inside as id.

You can simply get it without storing it inside.

String userIDstr = dataSnapshot.getKey();

Upvotes: 2

Related Questions