0

I'm using two event listeners one inside the other. The first is to check if the user is in the database , second to display the number of users in the database.

Query queries=database.child("songs").orderByChild("user").equalTo(userauth.getEmail());

ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        if(!dataSnapshot.exists()) {
            //create new user

            database.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    if (dataSnapshot.exists()) {
                        numOfUsers = dataSnapshot.child("users").getValue(int.class);
                        userinfo.setText(String.valueOf(numOfUsers));
                    }
                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {
                }
            });
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
queries.addValueEventListener(eventListener);

But I keep getting this error:


java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference
    at com.example.passiveincome.RegistrationActivity$1$1.onDataChange(RegistrationActivity.java:76)
    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:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6669)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

Here is a pic of my database: database.

I clearly have a field "users" which contains integers. Then where am I going wrong?

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

1 Answers1

0

The problem is here:

Query queries=database.child("songs").orderByChild("user").equalTo(userauth.getEmail());

There is no node songs at the root of the database, so database.child("songs") is not matching any nodes.

There also is no child property user. I assume you're looking for users (plural), but the database has no way of knowing that.

Your query needs to be:

database = FirebaseDatabase.getInstance().getReference();
Query queries=database.orderByChild("users").equalTo(0);
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • My apologies for not posting the complete database. I've added that now. You will see that the third object is in fact "songs" , also it has a child called "user" which contains the email of the user. –  Sep 19 '21 at 14:32
  • How is `database` initialized? – Frank van Puffelen Sep 19 '21 at 14:59
  • DatabaseReference database = FirebaseDatabase.getInstance().getReference(); –  Sep 19 '21 at 15:47
  • That leaves my answer the same: there is no child `songs` under the root, so there will be no data matching the query. I have a feeling you're trying to order/filter on `songs/*/user`, which isn't possible. For more on this see: https://stackoverflow.com/questions/27207059/firebase-query-double-nested and https://stackoverflow.com/questions/40656589/firebase-query-if-child-of-child-contains-a-value – Frank van Puffelen Sep 19 '21 at 16:24
  • Hey @GuitarGeorge Any update here? – Frank van Puffelen Sep 25 '21 at 20:09