Hansi
Hansi

Reputation: 97

Can't receive data from Firebase database

to make it short. The app crashes when I want to receive data from the Firebase database. Here is the code:

database = FirebaseDatabase.getInstance();
commu = database.getReference("Community");

longi = voteLoni.getText().toString();
lati = voteLati.getText().toString();
final String latiLong = lati + "  -  " + longi;

commu.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Community")
                    .child(latiLong);

            ref.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    count= dataSnapshot.child("count").getValue(long.class);
....

Structure: The childs of Community are coordinations

DB
  - Community 
    - 89.8 - 90.9
      - count: 4
      - example: 2
      - example2: 4
      - example3: 8
    - 120.9 - 89.9 
      - count: 45
      - example: 24
      - example2: 40
      - example3: 81

And that's the error:

Process: package, PID: 21441
java.lang.NullPointerException: Attempt to invoke virtual method 'long java.lang.Long.longValue()' on a null object reference
    at app.Activity.Real$2$1.onDataChange(Real.java:122)
    at com.google.firebase.database.Query$1.onDataChange(com.google.firebase:firebase-database@@16.0.3:183)
    at com.google.firebase.database.obfuscated.zzap.zza(com.google.firebase:firebase-database@@16.0.3:75)
    at com.google.firebase.database.obfuscated.zzca.zza(com.google.firebase:firebase-database@@16.0.3:63)
    at com.google.firebase.database.obfuscated.zzcd$1.run(com.google.firebase:firebase-database@@16.0.3:55)
    at android.os.Handler.handleCallback(Handler.java:789)
    at android.os.Handler.dispatchMessage(Handler.java:98)
    at android.os.Looper.loop(Looper.java:251)
    at android.app.ActivityThread.main(ActivityThread.java:6589)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

But in another activity I'm using the same code count= dataSnapshot.child("count").getValue(long.class); - to count the clicks of the user - and it is working...

Thanks in advance

Edit: I've added the second child and some values to the database. The DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Community").child(latiLong);is edited too, but this isn't correct?

Upvotes: 0

Views: 165

Answers (1)

Peter Haddad
Peter Haddad

Reputation: 80914

There is no need to write nested listeners..

If you have this structure:

DB
- Community 
  - 1
    - count: 4

And you do not have access the the child 1, then just do the following:

database = FirebaseDatabase.getInstance();
commu = database.getReference("Community");

commu.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
           for(DataSnapshot ds : dataSnapshot.getChildren()){
                count= ds.child("count").getValue(Long.class);
               }
           }
 .....

This will retrieve the value of count

Upvotes: 1

Related Questions