Reputation: 424
I'm attempting to obtain the current user's details by doing so:
Constants.USER_REF.orderByChild("email").equalTo(email).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists() && dataSnapshot != null) {
// User exists at this point, store it as currentUser variable.
User currentUser = dataSnapshot.getValue(User.class);
Log.i("THE_SNAPSHOT_AS_STRING:::", dataSnapshot.toString());
// GETTING THE ERROR ON THE LINE BELOW!
Log.i("THE_USERS_EMAIL:::", currentUser.getEmail());
} else {
// User does not exist at this point.
Toast.makeText(TourContactActivity.this, "No user exists.", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
I also get the following printed in the console log, so I know it's pulling in the proper user.
07-13 15:16:18.391 32277-32277/thepassapp.thompson.com.tomstours I/THE_SNAPSHOT_AS_STRING:::: DataSnapshot { key = users, value = {00845752-985f-4779-8eff-0c1e6a016ad8={tour_director_key=70af128e-777b-4c12-86f6-2952dbdc9185, last_name=River, [email protected], location_latitude=42.4305, tour_id=n1337e, photo=http://chopu.herokuapp.com/parse/files/2enxs2j21mz9/7304a8917568d366a8ec43f5ab88ac6b_user.jpg, tour_director_name=Chuck Thoms, middle_name=, location_longitude=-73.5103, passenger_id=mcu79, location_updated=1468360656387, tour_director=f9e3z, first_name=Gene, provider=password}} }
Here is my User.class
@JsonIgnoreProperties(ignoreUnknown=true)
public class User {
private String uid;
private String email;
private String first_name;
private String last_name;
public User() {
// empty default constructor, necessary for Firebase to be able to deserialize users
}
public User(String uid, String first_name, String last_name, String email) {
this.uid = uid;
this.first_name = first_name;
this.last_name = last_name;
this.email = email;
}
public String getEmail() {
return email;
}
public String getFirstName() {
return first_name;
}
public String getLastName() {
return last_name;
}
@Override
public String toString() {
return "User{uid='"+uid+"', email='"+email+"', first_name='"+first_name+"', last_name='"+last_name+"\'}";
}
}
My problem is actually retrieving this data from the User class. I followed the docs, but keep getting the error:
FATAL EXCEPTION: main
Process: passengerapp.veriguide.com.veriguidetours, PID: 32277
java.lang.NullPointerException: println needs a message
Any ideas on what I'm doing wrong? Note: I commented the line I'm obtaining the error on.
Upvotes: 1
Views: 1673
Reputation: 7720
You cannot directly call getValue()
because the dataSnapshot has childs, so you need to iterate the child before getting the value.
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
for (DataSnapshot data : dataSnapshot.getChildren()) {
User currentUser = data.getValue(User.class);
Log.i("THE_SNAPSHOT_AS_STRIN", data.toString());
Log.i("THE_USERS_EMAIL:::", currentUser.getEmail());
}
} else {
// User does not exist at this point.
}
}
And also, you have to create setter for your User
class so the value can be set to the class.
public void setEmail(String email) {
this.email = email;
}
public void setFirst_name(String first_name) {
this.first_name = first_name;
}
public void setLast_name(String last_name) {
this.last_name = last_name;
}
...
Hope this helps :)
Upvotes: 3
Reputation: 4719
Current User can be obtained by
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
userEmail = user.getEmail();
userName = user.getDisplayName();
}
Upvotes: 0