2

I'm trying to retrieve a child in my database so a user can join a group however when I do the method it gives me a NullPointerException because it goes to the return first how do I get it to go into the retrieval first?

public int groupCheck(String GrN, String Pass) {
    final String GN = GrN;

    DatabaseReference myRef = database.getReference("GROUPS").child("GROUP").child(GN);
    myRef.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            groupNam = dataSnapshot.getValue().toString();
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });


    if (groupNam.equals(GroupN)) {
        return 1;
    } else {
        return 0;
    }
}
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
Rcollier
  • 31
  • 2

3 Answers3

1

The ValueEventListeners are asynchronous, so they won't run first. The function won't wait for the listener to get a value. Instead, try calling a function that does what you wanted after getting the return value from inside the onDataChange() function.

zbys
  • 453
  • 2
  • 8
0

You can't add firebase data to a global variable. You should work inside of the onDataChange method.

public void groupCheck(String GrN, String Pass) {

final String GN = GrN;

DatabaseReference myRef = database.getReference("GROUPS").child("GROUP").child(GN);
myRef.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        groupNam = dataSnapshot.getValue().toString();

          if (groupNam.equals(GroupN)) {

         ////Do something. You can't return.

          } else {

           ////Do something. You can't return.

          }

    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});



}
0

Firebase APIs are asynchronous, meaning that onDataChange() method returns immediately after it's called, and the callback from the Task it returns, will be called some time later. There are no guarantees about how long it will take. It may take from a few hundred milliseconds, to a few seconds before that data is available and can be used. Because that method returns immediately, the value of your groupNam variable you're trying to use it outside the onDataChange() method, will not have been populated from the callback yet.

Basically, you're trying to return a value synchronously from an API that's asynchronous. That's not a good idea. You should handle the APIs asynchronously as intended.

A quick solve for this problem would be to move the following if statatemnt:

if (groupNam.equals(GroupN)) {
    return 1;
} else {
    return 0;
}

Inside the onDataChange() method and will work for sure, otherwise I recommend you see the last part of my anwser from this post in which I have explained how it can be done using a custom callback. You can also take a look at this video for a better understanding.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193