user9020006
user9020006

Reputation:

Value of a child in firebase database showing null

i usually work with firebase database like a constant 'Name' and a variable 'Value' but now according to my new project im trying to make both of it as variables which means that both aren't decided by me but the user... like this - https://ibb.co/cGh3qJ

My error - https://ibb.co/hiJ4xy

Code

Fetching Data

Results results;
String Name, Score;
private void fetchResults() {
    mDatabaseReference.child("Users").child(id).child("Quiz").child("Results").child(id).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot childDataSnapshot : dataSnapshot.getChildren()) {
                Name = childDataSnapshot.getKey().toString();
                Score = String.valueOf(childDataSnapshot.child(Name).getValue(Results.class));
                results = new Results(Name, Score);
                resultsList.add(results);
                mAdapter.notifyDataSetChanged();
            }
            mAdapter.notifyDataSetChanged();

                    }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            throw databaseError.toException();
        }
    });
}

Class

public class Results {
private String Name;
private String Score;

public Results() {
}

public Results(String Name, String Score) {
    this.Name = Name;
    this.Score = Score;
}

public String getName() {
    return Name;
}

public void setName(String name) {
    Name = name;
}

public String getScore() {
    return Score;
}

public void setScore(String score) {
    Score = score;
}

}

Can anyone point out where i have gone wrong please... before i used to not get the name also and somehow i managed to get it and now struggling with the score

Error

Process: com.appmaster.akash.messageplus, PID: 8881
                                             java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
                                                 at com.appmaster.akash.messageplus.TakingQuizDone$1.onDataChange(TakingQuizDone.java:68)
                                                 at com.google.android.gms.internal.zzduz.zza(Unknown Source)
                                                 at com.google.android.gms.internal.zzdwu.zzbvb(Unknown Source)
                                                 at com.google.android.gms.internal.zzdxa.run(Unknown Source)
                                                 at android.os.Handler.handleCallback(Handler.java:742)
                                                 at android.os.Handler.dispatchMessage(Handler.java:95)
                                                 at android.os.Looper.loop(Looper.java:157)
                                                 at android.app.ActivityThread.main(ActivityThread.java:5571)
                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:745)
                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:635)

Upvotes: 1

Views: 1166

Answers (2)

Parth Bhoiwala
Parth Bhoiwala

Reputation: 1322

You need to change

Score = String.valueOf(childDataSnapshot.child(Name).getValue(Results.class));

into this:

Score = (String) childDataSnapshot.getValue();

The problem with your implementation was that you were already iterating over the children of dataSnapshot. So you didn't need to give the key and get the value, the childDataSnapshot has the value.

Upvotes: 0

Peter Haddad
Peter Haddad

Reputation: 80944

Change this:

 Score = String.valueOf(childDataSnapshot.child(Name).getValue(Results.class));

into this:

  Score =childDataSnapshot.child(Name).getValue().toString();

Your datasnapshot is at the userid, then you iterated inside of it and used getKey() to be able to retrieve the name, then to retrieve the value just get the value of the child Name.

Upvotes: 1

Related Questions