Android - dataSnapshot.getValue () returns null in second call (but not first)

I am trying to loop through each index of the People child to set the snapshot value to the provider inside the adapter. When the user looks at the adapter, they can see people's names, age, hair color, and eye color.

Firebase base structure (linked as I cannot insert images yet)

My function

public void getInfo() {


 ValueEventListener profileListener = new ValueEventListener() {


    @Override
     public void onDataChange(DataSnapshot dataSnapshot) {


       totalPeople = dataSnapshot.child("People").getChildrenCount();            


       for (int personIndex = 0; personIndex < totalPeople; personIndex++){


          String name =  dataSnapshot.child("People").child(String.valueOf(personIndex)).child("name").getValue().toString();


          String age =  dataSnapshot.child("People").child(String.valueOf(personIndex)).child("age").getValue().toString();


          String hairColor =  dataSnapshot.child("People").child(String.valueOf(personIndex)).child("hairColor").getValue().toString();


          String eyeColor =  dataSnapshot.child("People").child(String.valueOf(personIndex)).child("eyeColor").getValue().toString();


          PersonProvider personProvider = new PersonProvider(name, age, hairColor, eyeColor);

          personActivityAdapter.add(personProvider);
       }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
                Log.e(TAG, "fetching data from firebase failed");
    }
 }; 

 myRef.child("users").child(userId).addValueEventListener(profileListener);
}

      

Mistake

06-06 11:38:21.900 31357-31357/com.RentRedi.RentRedi2 E/UncaughtException: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
    at com.RentRedi.RentRedi2.VehicleActivity$10.onDataChange(VehicleActivity.java:380)
    at com.google.android.gms.internal.zzbpx.zza(Unknown Source)
    at com.google.android.gms.internal.zzbqx.zzZV(Unknown Source)
    at com.google.android.gms.internal.zzbra$1.run(Unknown Source)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6121)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)

      

Attempts

When I change:

String name =  dataSnapshot.child("People").child(String.valueOf(personIndex)).child("name").getValue().toString();

      

in

String name =  dataSnapshot.child("People").child(String.valueOf(personIndex)).child("name").toString();

      

it returns the entire dataSnapshot as a string with all its keys and values. However, when I add .getValue()

, I get a null pointer exception when firstIndex is hit the first time.

Note: I also switched the order of the snapshots (for example, put first age

, first hairColor

or eyeColor

. It always gets the value of the first one, but fails in the second, specifying that the value is null.

Why does it return null in the second call, but not the first, no matter which dataSnapshot box I put first?

+3


source to share





All Articles