试图从我的Firebase数据库中拉出所有用户,然后将其显示在我的应用程序上,但出现错误提示

No setter/field for id found on class com.example.chatappfirebase.Model.User


但是我的User类getter和setter是从android studio生成的

我的用户类别代码:

package com.example.chatappfirebase.Model;


public class User {

    private String userId;
    private String username;
    private String ImageURL;

    public User(String userId, String username, String imageURL) {
        this.userId = userId;
        this.username = username;
        ImageURL = imageURL;
    }

    public User() {
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getImageURL() {
        return ImageURL;
    }

    public void setImageURL(String imageURL) {
        ImageURL = imageURL;
    }
}


当我尝试从数据库中拉出用户列表时

    final FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
    DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Users");
    reference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            users.clear();
            for(DataSnapshot snapshot: dataSnapshot.getChildren()) {
                User user = snapshot.getValue(User.class);
                    assert user != null;
                    assert firebaseUser != null;
                    if (!user.getUserId().equals(firebaseUser.getUid())) {
                        users.add(user);
                    }

            }

            userAdapter = new UserAdapter(getContext(), users);


   recyclerView.setAdapter(userAdapter);
            }


java - 如何修复在com.example.chatappfirebase.Model.User类上找不到ID的setter/field?-LMLPHP

 java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
        at com.example.chatappfirebase.Fragments.UsersFragment$1.onDataChange(UsersFragment.java:65)


这是这行代码:

if (!user.getUserId().equals(firebaseUser.getUid())) {}

最佳答案

阅读此answer以获得解释。

public class User {

    private String id;
    private String username;
    private String imageURL;

    public User(String id, String username, String imageURL) {
        this.id = id;
        this.username = username;
        imageURL = imageURL;
    }

    public User() {
    }

    public String getId() {
        return id;
    }

    public void setUserId(String id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getImageURL() {
        return imageURL;
    }

    public void setImageURL(String imageURL) {
        imageURL = imageURL;
    }
}

10-07 22:37