skip to Main Content

the application worked fine, but then it started crashing when I started the given activity

private void loadSpins() {
    reference.child(FirebaseAuth.getInstance().getUid()).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
            UserModelTask modelTask = snapshot.getValue(UserModelTask.class);
            if (snapshot.exists()) {
                currentSpin = modelTask.getSpins();
                binding.currentSp.setText(String.valueOf(currentSpin));
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {

        }
    });
}

FATAL EXCEPTION: main Process: com.example.cashapp, PID: 1777 com.google.firebase.database.DatabaseException: Failed to convert value of type java.lang.Boolean to String at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertString(CustomClassMapper.java:426) at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(CustomClassMapper.java:217) at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToType(CustomClassMapper.java:179) at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.access$100(CustomClassMapper.java:48) at com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.deserialize(CustomClassMapper.java:593) at com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.deserialize(CustomClassMapper.java:563) at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertBean(CustomClassMapper.java:433) at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(CustomClassMapper.java:232) at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertToCustomClass(CustomClassMapper.java:80) at com.google.firebase.database.DataSnapshot.getValue(DataSnapshot.java:202) at com.example.cashapp.SpinnerActivity$3.onDataChange(SpinnerActivity.java:191) at com.google.firebase.database.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:75) at com.google.firebase.database.core.view.DataEvent.fire(DataEvent.java:63) at com.google.firebase.database.core.view.EventRaiser$1.run(EventRaiser.java:55) at android.os.Handler.handleCallback(Handler.java:942) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loopOnce(Looper.java:201) at android.os.Looper.loop(Looper.java:288) at android.app.ActivityThread.main(ActivityThread.java:7872) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)

UserModelTask.class for this code

public class UserModelTask {
    private int scratch, spins;
    private String referCode, redeemStatus;

    public UserModelTask(int spins, String scratch, String referCode, String redeemStatus) {
        this.spins = spins;
        this.referCode = referCode;
        this.redeemStatus = redeemStatus;
    }

    public UserModelTask() {
    }

    public int getSpins() {
        return spins;
    }

    public void setSpins(int spins) {
        this.spins = spins;
    }
    public String getReferCode() {
        return referCode;
    }
    public String getRedeemStatus() {
        return redeemStatus;
    }

    public void setRedeemStatus(String redeemStatus) {
        this.redeemStatus = redeemStatus;
    }

    public void setReferCode(String referCode) {
        this.referCode = referCode;
    }    
}

as I said before, it worked normally and now it’s starting to crush me even though I reset the database

firebase

what exactly happened and exactly what I should do

2

Answers


  1. Are you sure about the

    snapshot.getValue(UserModelTask.class)

    ? i am not sure if you can get types other than standard ones like Boolean String… from firebase

    Login or Signup to reply.
  2. The redeemStatus value in your database screenshot is a boolean value, but in your UserModelTask class you’ve defined it as a string. The Firebase SDK is looking for a boolean field/property redeemStatus, and since the field in your class doesn’t match the type, it raises the error you get.

    The solution is to modify your Java class to match the type in your database, or to modify the database to match the type in your code. That last one is easiest, as you can change the database to:

    redeemStatus: "false"
    

    If you want to modify the code instead, that’d be:

    public class UserModelTask {
        private int scratch, spins;
        private String referCode;
        private bool redeemStatus; // 👈
    
        public UserModelTask(int spins, String scratch, String referCode, bool redeemStatus) { // 👈
            this.spins = spins;
            this.referCode = referCode;
            this.redeemStatus = redeemStatus;
        }
    
        public UserModelTask() {
        }
    
        public int getSpins() {
            return spins;
        }
    
        public void setSpins(int spins) {
            this.spins = spins;
        }
        public String getReferCode() {
            return referCode;
        }
        public bool getRedeemStatus() { // 👈
            return redeemStatus;
        }
    
        public void setRedeemStatus(bool redeemStatus) { // 👈
            this.redeemStatus = redeemStatus;
        }
    
        public void setReferCode(String referCode) {
            this.referCode = referCode;
        }    
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search