Jonathon
Jonathon

Reputation: 1601

Getting values from Firebase

I'm new to Firebase, and decided to get my feet wet. However, I'm having trouble retrieving values from a query. I'm basically trying to get the password value, but I believe it's returning nothing.

Image

Error:

java.lang.NullPointerException: println needs a message

loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String username = usernameField.getText().toString();
                String password = passwordField.getText().toString();

                db.child("Users").orderByChild("username").equalTo(username).addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        if (dataSnapshot.exists()) {
                            //Get the password,and check it against the password field
                            Map<String, String> map = (Map<String, String>)dataSnapshot.getValue();

                            Log.d("result", map.get("password"));

                        } else {
                            Toast.makeText(getApplicationContext(), "Incorrect login details", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });
            }
        });

I know this isn't very practical, but like I said I'm only practicing.

Thanks for the help!

Upvotes: 0

Views: 61

Answers (2)

Frank van Puffelen
Frank van Puffelen

Reputation: 600090

When you execute a query against the Firebase Database, there will potentially be multiple results. So the snapshot contains a list of those results. Even if there is only a single result, the snapshot will contain a list of one result.

The code in your onDataChange needs to take care of the fact that the snapshot is a list, by looping over its snapshot.getChildren():

db.child("Users").orderByChild("username").equalTo(username).addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot snapshot: dataSnapshot.getChildren()) {
            //Get the password,and check it against the password field
            Map<String, String> map = (Map<String, String>)snapshot.getValue();

            Log.d("result", map.get("password"));
        }
    }

Upvotes: 1

Gast&#243;n Saill&#233;n
Gast&#243;n Saill&#233;n

Reputation: 13159

Sine your database structure is different than your reference in your code you should change some things

First your Reference to the data should be like this

 db.child("Users").child(uid).child("password").addValueEventListener...

Where uid is the result of

 private FirebaseAuth mAtuh;

 mAuth = FirebaseAuth.getCurrentUser().getUid();

 String uid = mAuth ;

Check the official doc on how to properly authenticate users

https://firebase.google.com/docs/auth/android/manage-users

Also you can implement google sign-in :

https://firebase.google.com/docs/auth/android/google-signin

And then you can retrieve your password field like this

// Attach a listener to read the data
ref.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        String password = dataSnapshot.getValue(String.class);
        Log.e("The password is:",""+password);
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
      Log.e("Error getting the password",""+databaseError.getCode());
    }
});

Hardcoding your reference without mAuth will be like this

db.child("Users").child("L7VR2mGZKnbReFqOlmP").child("password").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            if (dataSnapshot.exists()) {

            String password = dataSnapshot.getValue(String.class);
            Log.e("The password is:",""+password);

           }else
              {

              Toast.makeText(getApplicationContext(), "Incorrect login details", Toast.LENGTH_SHORT).show();

             }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
          Log.e("Error getting the password",""+databaseError.getCode());
        }
    });

Hope it helps

happy coding

Upvotes: 0

Related Questions