Firebase DatabaseException "Can't convert object of type java.util.ArrayList"

Viewed 689

My Database StructureIm getting a Firebase DatabaseException "Can't convert object of type java.util.ArrayList to type com.example.firebasedeneme.Title "

Im having tihs error by getValue()

a = FirebaseDatabase.getInstance().getReference("Users");
        but.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                a.addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                        if(dataSnapshot.child(e.getText().toString()).exists()){
                            String username = e.getText().toString();
                            User user = dataSnapshot.child(username).getValue(User.class);

There i save the datas

FirebaseDatabase.getInstance().getReference("Users").child(user.getUsername()).setValue(user);

My User class looks like

public class User implements Comparable<User>,Serializable{
    private String username;
    private String password;
    private String title;
    private int xp;
    private int score;
    private int level;
    private int image;
    private Stack<Title> titles;
    public User() {}
    public User(String username, String password, String title, int xp, int score, int level, int image, Stack<Title> titles){
        this.username = username;
        this.password = password;
        this.level = level;
        this.score = score;
        this.xp = xp;
        this.title = title;
        this.image = image;
        this.titles = titles;
    }

My Title Class looks like

public class Title {
    private int titleno;
    private String title;
    public Title(int titleno, String title){
        this.title = title;
        this.titleno = titleno;
    }
    public int getTitleno(){
        return this.titleno;
    }
    public String getTitle(){
        return this.title;
    }
}

How can I fix this problem?

2 Answers

I think that it is because the type of Title is a Stack and in Firebase you have an Array. Change your Title variable to be:

private ArrayList<Title> titles;

If that doesn't work, can you also add your Title model class to the question? That way we might be able to help better.

Related