Skip to content
Advertisement

Getting error : Could not deserialize object. Failed to convert a value of type java.lang.String to long

enter image description hereI have tried changing values many times. Now I have the same values in Firebas. It’s working if I give the values manually in code, but it’s not working while I try to get the info from Firestore. But still, I’m getting this error continuously:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.luteraa.luteraaesports, PID: 8828
java.lang.RuntimeException: Could not deserialize object. Failed to convert a value of type java.lang.String to long (found in field 'matchNumber')
    at com.google.firebase.firestore.util.CustomClassMapper.deserializeError(CustomClassMapper.java:563)
    at com.google.firebase.firestore.util.CustomClassMapper.convertLong(CustomClassMapper.java:434)
    at com.google.firebase.firestore.util.CustomClassMapper.deserializeToPrimitive(CustomClassMapper.java:326)
    at com.google.firebase.firestore.util.CustomClassMapper.deserializeToClass(CustomClassMapper.java:226)
    at com.google.firebase.firestore.util.CustomClassMapper.deserializeToType(CustomClassMapper.java:189)
    at com.google.firebase.firestore.util.CustomClassMapper.access$300(CustomClassMapper.java:54)
    at com.google.firebase.firestore.util.CustomClassMapper$BeanMapper.deserialize(CustomClassMapper.java:770)
    at com.google.firebase.firestore.util.CustomClassMapper$BeanMapper.deserialize(CustomClassMapper.java:741)
    at com.google.firebase.firestore.util.CustomClassMapper.convertBean(CustomClassMapper.java:542)
    at com.google.firebase.firestore.util.CustomClassMapper.deserializeToClass(CustomClassMapper.java:253)
    at com.google.firebase.firestore.util.CustomClassMapper.convertToCustomClass(CustomClassMapper.java:100)
    at com.google.firebase.firestore.DocumentSnapshot.toObject(DocumentSnapshot.java:183)
    at com.google.firebase.firestore.QueryDocumentSnapshot.toObject(QueryDocumentSnapshot.java:116)
    at com.google.firebase.firestore.DocumentSnapshot.toObject(DocumentSnapshot.java:161)
    at com.google.firebase.firestore.QueryDocumentSnapshot.toObject(QueryDocumentSnapshot.java:97)
    at com.luteraa.luteraaesports.BgmiActivity$1.onEvent(BgmiActivity.java:39)
    at com.luteraa.luteraaesports.BgmiActivity$1.onEvent(BgmiActivity.java:35)
    at com.google.firebase.firestore.Query.lambda$addSnapshotListenerInternal$2$Query(Query.java:1133)
    at com.google.firebase.firestore.-$$Lambda$Query$JWhMgzcsIac1Z-exZj1pTDRisJg.onEvent(Unknown Source:6)
    at com.google.firebase.firestore.core.AsyncEventListener.lambda$onEvent$0$AsyncEventListener(AsyncEventListener.java:42)
    at com.google.firebase.firestore.core.-$$Lambda$AsyncEventListener$DNkggu2LY54oguDvcp-QtRg6Sfg.run(Unknown Source:6)
    at android.os.Handler.handleCallback(Handler.java:914)
    at android.os.Handler.dispatchMessage(Handler.java:100)
    at android.os.Looper.loop(Looper.java:224)
    at android.app.ActivityThread.main(ActivityThread.java:7551)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:995)

That’s my Model’s code

public class BGMICategoryModel {
private String matchId;
private long matchNumber;
private String gameMode;

public BGMICategoryModel(String matchId, long matchNumber, String gameMode) {
    this.matchId = matchId;
    this.matchNumber = matchNumber;
    this.gameMode = gameMode;
}
public  BGMICategoryModel(){}

public String getMatchId() {
    return matchId;
}

public void setMatchId(String matchId) {
    this.matchId = matchId;
}

public long getMatchNumber() {
    return matchNumber;
}

public void setMatchNumber(long matchNumber) {
    this.matchNumber = matchNumber;
}

public String getGameMode() {
    return gameMode;
}

public void setGameMode(String gameMode) {
    this.gameMode = gameMode;
}

}

My adapter code

public class BGMICategoryAdapter extends RecyclerView.Adapter<BGMICategoryAdapter.BGMICategoryViewHolder> {

Context context;
ArrayList<BGMICategoryModel> bgmiCategoryModels;


public BGMICategoryAdapter(Context context, ArrayList<BGMICategoryModel> bgmiCategoryModels){

    this.context = context;
    this.bgmiCategoryModels = bgmiCategoryModels;
}

@NonNull
@NotNull
@Override
public BGMICategoryViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(context).inflate(R.layout.matches_bgmi,null);
    return new BGMICategoryViewHolder(view);
}


@Override
public void onBindViewHolder(@NonNull @NotNull BGMICategoryAdapter.BGMICategoryViewHolder holder, int position) {

    BGMICategoryModel model = bgmiCategoryModels.get(position);

    holder.matchNumber.setText(String.valueOf(model.getMatchNumber()));
    holder.gameMode.setText(model.getGameMode());
}

@Override
public int getItemCount() {
    return bgmiCategoryModels.size();
}

public class BGMICategoryViewHolder extends RecyclerView.ViewHolder{
    TextView matchNumber, gameMode;

    public BGMICategoryViewHolder(@NonNull @NotNull View itemView) {
        super(itemView);
        matchNumber = itemView.findViewById(R.id.matchNumber);
        gameMode = itemView.findViewById(R.id.gameMode);
    }
}

}

Main Activity

public class BgmiActivity extends AppCompatActivity {
ActivityBgmiBinding activityBgmiBinding;
FirebaseFirestore fStore;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    activityBgmiBinding = ActivityBgmiBinding.inflate(getLayoutInflater());
    setContentView(activityBgmiBinding.getRoot());

    fStore = FirebaseFirestore.getInstance();

    ArrayList<BGMICategoryModel> bgmiCategoryModels = new ArrayList<>();
    //bgmiCategoryModels.add(new BGMICategoryModel("match1", 1, "PCM"));
    BGMICategoryAdapter adapter = new BGMICategoryAdapter(this, bgmiCategoryModels);

    fStore.collection("bgmiMatches").addSnapshotListener(new EventListener<QuerySnapshot>() {
        @Override
        public void onEvent(@Nullable @org.jetbrains.annotations.Nullable QuerySnapshot value, @Nullable @org.jetbrains.annotations.Nullable FirebaseFirestoreException error) {
            for (DocumentSnapshot snapshot : value.getDocuments()){
                BGMICategoryModel model = snapshot.toObject(BGMICategoryModel.class);
                model.setMatchId(snapshot.getId());
                bgmiCategoryModels.add(model);
            }
            adapter.notifyDataSetChanged();
        }
    });
    activityBgmiBinding.bgmiContent.setLayoutManager(new LinearLayoutManager(this));
    activityBgmiBinding.bgmiContent.setAdapter(adapter);
}

}

I cannot find a solution for this. Please help me fix this. Thank you.

Advertisement

Answer

You are getting the following error:

java.lang.RuntimeException: Could not deserialize object. Failed to convert a value of type java.lang.String to long (found in field ‘matchNumber’)

Because your matchNumber field is defined in your class as a long, while in database holds a String value. To get rid of this Exception, please change the type of the field to be a Number and not a String, for example:

matchNumber: 12 //Correct

matchNumber: "12" //Incorrect. See the quotation marks?
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement