0

I am using firebase database for my app and I saved my users in it. This is my UserInformation class:

public class UserInformation {


private String email1;
private String Password1;
public HashMap<String, Boolean> SayingsLevels=new HashMap<>();
public HashMap<String, Boolean> songs=new HashMap<>();

public UserInformation (String email, String Password){
    this.email1=email;
    this.Password1=Password;
    SayingsLevels.put("1", false);
    SayingsLevels.put("2", false);
    SayingsLevels.put("3", false);
    songs.put("1", false);
    songs.put("2", false);
    songs.put("3", false);

}

And I saved it with this code on my main activity:

firebaseUser=FirebaseAuth.getInstance().getCurrentUser();
                UserInformation user=new UserInformation(email1,password1);
                String id=firebaseUser.getUid();
                mDatabaseReference.child("user").child(id).setValue(user);

Now, I want the get the hashmap data that was stored in my database. I used the following code:

mDatabase.addValueEventListener(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            mDatabase = FirebaseDatabase.getInstance().getReference();
            firebaseuser=FirebaseAuth.getInstance().getCurrentUser();
            id=firebaseuser.getUid();

           for(DataSnapshot dataSnapshot1: dataSnapshot.getChildren() ){
               UserInformation user= dataSnapshot1.child("user").child(id).getValue(UserInformation.class);

            }

but on variable user I'm getting all the time null, what I did wrong? Thanks for help!

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
Elior Sastiel
  • 1,074
  • 2
  • 10
  • 21
  • Please share us your database structure. – Alex Mamo Aug 27 '17 at 08:21
  • @Elior: You can get this database structure by clicking the "Export JSON" link in your [Firebase Database console](https://console.firebase.google.com/project/_/database/data). Having it as text is more useful than a screenshot. – Frank van Puffelen Aug 27 '17 at 08:48

1 Answers1

1

You need to initialize mDatabase before attaching a listener to it:

mDatabase = FirebaseDatabase.getInstance().getReference();
mDatabase.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        firebaseuser=FirebaseAuth.getInstance().getCurrentUser();
        id=firebaseuser.getUid();

       for(DataSnapshot dataSnapshot1: dataSnapshot.getChildren() ){
           UserInformation user= dataSnapshot1.child("user").child(id).getValue(UserInformation.class);

        }

But note that this code is going to download your entire database, so find the data for a single user. As your app's user base grows, you'll be downloading more and more data that is not needed.

I recommend only downloading the current user's data like this:

mDatabase = FirebaseDatabase.getInstance().getReference();
firebaseuser=FirebaseAuth.getInstance().getCurrentUser();
id=firebaseuser.getUid();

mDatabase.child("users").child(id).addValueEventListener(new ValueEventListener() {

    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        UserInformation user= dataSnapshot.getValue(UserInformation.class);

Finally: I'd recommend learning how to troubleshoot NullPointerExceptions by studying this and related questions: What is a NullPointerException, and how do I fix it?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807