Wilson Kwun
Wilson Kwun

Reputation: 87

Firebase unable to retrieve value to class

I have a working line of code retrieving each single data, but failed to apply them all to a class. This line is working perfectly.

double value = (double) ds.child("player1score").getValue();

But when it comes to this, it fails and crashes.

int value = (int) ds.child("point").getValue();

Solved this problem by changing it into:

long value = (long ) ds.child("point").getValue();

Yet, when retrieving using class, error occurs. Here are the whole picture of my codes, appreciate so much for any advice, thanks!!

Round().class

public class Round {
public double player1score;
public double player2score;
public double player3score;
public double player4score;
public int point;

//Constructor
public Round(double player1score, double player2score, double player3score, double player4score, int point) {
    this.player1score = player1score;
    this.player2score = player2score;
    this.player3score = player3score;
    this.player4score = player4score;
    this.point = point;

//Below are All the getter and setter etc
}

My MainActivity.class.onCreate()

//Declare Variables
UserId = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference gameInfoRef = rootRef.child("user").child(UserId).child("gameInfo");
DatabaseReference gameRecordRef = rootRef.child("user").child(UserId).child("gameRecord");

String gameKey = "-LLyXhetqj9mj5fgh9bn";

Under onCreate():

gameRecordRef.child(gameKey).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot ds : dataSnapshot.getChildren()) {
                ListView lv_history = (ListView) findViewById(R.id.lv_history);

                //It shows proper results in logcat
                System.out.println(ds.getValue());

                ArrayList<Round> ResultList = new ArrayList<>();

                Round round = (Round) ds.getValue(Round.class);

                ResultList.add(round);

                ivd_HistoryAdapter adapter = new ivd_HistoryAdapter(id_History.this, ResultList);
                lv_history.setAdapter(adapter);
            }
        }

Firebase structure in text:

  "user": 
       "5xGKRXeHgThQy70lduPEp3mosTj1": 
             "gameRecord": 
                   "-LLyXhetqj9mj5fgh9bn": 
                        "player1score": 0.5,
                        "player2score": 0.5,
                        "player3score": 0.5,
                        "player4score": 0.5,
                        "point": 5

Logcat Error: (Edited, not causing by integer type error)

 com.google.firebase.database.DatabaseException: Class viwil.mahjongcal.Round does not define a no-argument constructor. If you are using ProGuard, make sure these constructors are not stripped.
    at com.google.android.gms.internal.zzelx.zze(Unknown Source)
    at com.google.android.gms.internal.zzelw.zzb(Unknown Source)
    at com.google.android.gms.internal.zzelw.zza(Unknown Source)
    at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
    at viwil.mahjongcal.id_History$1.onDataChange(id_History.java:51)
    at com.google.android.gms.internal.zzegf.zza(Unknown Source)
    at com.google.android.gms.internal.zzeia.zzbyc(Unknown Source)
    at com.google.android.gms.internal.zzeig.run(Unknown Source)
    at android.os.Handler.handleCallback(Handler.java:761)
    at android.os.Handler.dispatchMessage(Handler.java:98)
    at android.os.Looper.loop(Looper.java:156)
    at android.app.ActivityThread.main(ActivityThread.java:6523)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)

Error pointed to this line: (id_History.java:51)

    Round round = ds.getValue(Round.class);

Firebase screenshot:

enter image description here

Upvotes: 2

Views: 1023

Answers (1)

Alex Mamo
Alex Mamo

Reputation: 138824

To solve this, please change the following line of code:

int value = (int) ds.child("point").getValue();

to

long score = ds.child("point").getValue(Long.class);

You need to cast that object to an object of type Long and not to a primitive int. Even if you are defining your score property in your model class as an int, in the database that property is stored as a long. By default, the numbers are stored in the Firebase Realtime Database as long numbers and not as ints.

Upvotes: 2

Related Questions