Home » Java » DatabaseException – Can't convert object of type java.lang.String to type com.appmaster.akash.messageplus.Results

DatabaseException – Can't convert object of type java.lang.String to type com.appmaster.akash.messageplus.Results

Posted by: admin July 12, 2018 Leave a comment

Questions:

Error

FATAL EXCEPTION: main
 Process: com.appmaster.akash.messageplus, PID: 978
 com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.appmaster.akash.messageplus.Results
     at com.google.android.gms.internal.zzear.zzb(Unknown Source)
     at com.google.android.gms.internal.zzear.zza(Unknown Source)
     at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
     at com.appmaster.akash.messageplus.TakingQuizDone$1.onChildAdded(TakingQuizDone.java:65)
     at com.google.android.gms.internal.zzdri.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)

Code

private void fetchResults() {
        mDatabaseReference.child("Users").child(id).child("Quiz").child("Results").addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                Results results = dataSnapshot.getValue(Results.class);
                resultsList.add(results);
                mAdapter.notifyDataSetChanged();
            }

            @Override
            public void onChildChanged(DataSnapshot dataSnapshot, String s) {
            }

            @Override
            public void onChildRemoved(DataSnapshot dataSnapshot) {
            }

            @Override
            public void onChildMoved(DataSnapshot dataSnapshot, String s) {
            }

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

Database

enter image description here

Results.java

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;
}

}

The error is in the database both the name and the value isn’t fixed. Like its decided according to the user.

Saving the data

String name = RecieversName;
                HashMap<String, String> userMap = new HashMap<>();
                userMap.put(name, String.valueOf(mScore));
                mRef.child("Users").child(RecieversId).child("Quiz").child("Results").setValue(userMap).addOnCompleteListener(new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {
                        if (task.isSuccessful()) {
                            Intent intent = new Intent(TakingQuiz.this, TakingQuizDone.class);
                            intent.putExtra("RecieversId",RecieversId);
                            intent.putExtra("Score", mScore.toString());
                            startActivity(intent);
                            finish();

                        }
                    }
                });
Answers: