Android - java Cloud Firestore:将数据转换为Object时出现NullPointerException

时间:2018-01-27 10:38:47

标签: java android nullpointerexception google-cloud-firestore

这就是我的Firestore数据结构的样子

你好,我有一个问题,我在网上搜索过,但似乎没有什么能接近我面临的问题。我正在使用firestore来存储我的数据

这是用户节点。 user section

这是user_account_settings节点。 user_account_settings section

目标是遍历这些节点并从节点检索/获取数据并将其设置为文本并使用自定义模型显示它们。

这是帐户设置的模型

公共类UserAccountSettings {

private String description;
private String display_name;
private long followers;
private long following;
private long posts;
private String profile_photo;
private String username;
private String website;


public UserAccountSettings(String description, String display_name, long followers, long following,
                           long posts, String profile_photo, String username, String website) {
    this.description = description;
    this.display_name = display_name;
    this.followers = followers;
    this.following = following;
    this.posts = posts;
    this.profile_photo = profile_photo;
    this.username = username;
    this.website = website;
}

public UserAccountSettings(){

}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

public String getDisplay_name() {
    return display_name;
}

public void setDisplay_name(String display_name) {
    this.display_name = display_name;
}

public long getFollowers() {
    return followers;
}

public void setFollowers(long followers) {
    this.followers = followers;
}

public long getFollowing() {
    return following;
}

public void setFollowing(long following) {
    this.following = following;
}

public long getPosts() {
    return posts;
}

public void setPosts(long posts) {
    this.posts = posts;
}

public String getProfile_photo() {
    return profile_photo;
}

public void setProfile_photo(String profile_photo) {
    this.profile_photo = profile_photo;
}

public String getUsername() {
    return username;
}

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

public String getWebsite() {
    return website;
}

public void setWebsite(String website) {
    this.website = website;
}

@Override
public String toString() {
    return "UserAccountSettings{" +
            "description='" + description + '\'' +
            ", display_name='" + display_name + '\'' +
            ", followers=" + followers +
            ", following=" + following +
            ", posts=" + posts +
            ", profile_photo='" + profile_photo + '\'' +
            ", username='" + username + '\'' +
            ", website='" + website + '\'' +
            '}';
}

}

和用户模型

public class User {
private String user_id;
private long phone_number;
private String email;
private String username;

public User(String user_id, long phone_number, String email, String username) {
    this.user_id = user_id;
    this.phone_number = phone_number;
    this.email = email;
    this.username = username;
}

public User(){

}

public String getUser_id() {
    return user_id;
}

public void setUser_id(String user_id) {
    this.user_id = user_id;
}

public long getPhone_number() {
    return phone_number;
}

public void setPhone_number(long phone_number) {
    this.phone_number = phone_number;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getUsername() {
    return username;
}

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

@Override
public String toString() {
    return "User{" +
            "user_id='" + user_id + '\'' +
            ", phone_number='" + phone_number + '\'' +
            ", email='" + email + '\'' +
            ", username='" + username + '\'' +
            '}';
}

}

用户设置(同时持有user和user_account_settings)

public class UserSettings {
//for handling multilple queries in database : users and user_account_settings.
private User user;
private UserAccountSettings settings;

public UserSettings(User user, UserAccountSettings settings) {
    this.user = user;
    this.settings = settings;
}

public UserSettings(ArrayList<User> mUser, ArrayList<UserAccountSettings> mSettings){

}

public User getUser() {
    return user;
}

public void setUser(User user) {
    this.user = user;
}

public UserAccountSettings getSettings() {
    return settings;
}

public void setSettings(UserAccountSettings settings) {
    this.settings = settings;
}

@Override
public String toString() {
    return "UserSettings{" +
            "user=" + user +
            ", settings=" + settings +
            '}';
}

}

尝试检索并将数据从firestore设置为模型

public UserSettings getUserSettings(final DocumentSnapshot documentSnapshot){
    Log.d(TAG, "getUserSettings: retrieving user account settings from firestore");

    final ArrayList<UserAccountSettings> mSettings = new ArrayList<>();
    final ArrayList<User> mUser = new ArrayList<>();


        //handling user_account_settings_node.
        mFirestore.collection("user_account_settings").addSnapshotListener(new EventListener<QuerySnapshot>() {
        @Override
        public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
            if (e != null){
                Log.w(TAG, "onEvent: FirebaseFirestoreException", e);
                return;
            }
            for (DocumentSnapshot document : documentSnapshots) {
                Log.d(TAG, document.getId() + " => " + document.getData());
                if (document.getId().equals("user_account_settings")){
                    Log.d(TAG, "checking if id equals user_account_settings: yes, it does");
                    Log.d(TAG, "onSuccess: Document" + document.getId() + ";" + document.getData());

                        UserAccountSettings settings = document.toObject(UserAccountSettings.class);
                    Log.d(TAG, "UserAccountSettings: " + "username is" + settings.getUsername());


                    settings.setDisplay_name(documentSnapshot.getString("display_name"));
                    settings.setUsername(documentSnapshot.getString("username"));
                    settings.setWebsite(documentSnapshot.getString("website"));
                    settings.setProfile_photo(documentSnapshot.getString("profile_photo"));
                    settings.setPosts(documentSnapshot.getLong("posts"));
                    settings.setFollowers(documentSnapshot.getLong("followers"));
                    settings.setFollowing(documentSnapshot.getLong("following"));
                    mSettings.add(settings);

                    }
                }

            }
    });

    //handling users node.
   mFirestore.collection("users").addSnapshotListener(new EventListener<QuerySnapshot>() {
        @Override
        public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
            if (e != null){
                Log.w(TAG, "onEvent: ", e);
                return;
            }
            for (DocumentSnapshot document : documentSnapshots) {
                Log.d(TAG, document.getId() + " => " + document.getData());
                if (document.getId().equals(mContext.getString(R.string.dbname_users))){
                    Log.d(TAG, "checking if id equals user_account_settings: yes, it does");
                    Log.d(TAG, "onSuccess: Document" + document.getId() + ";" + document.getData());

                    User user = document.toObject(User.class);
                    Log.d(TAG, "user: got the user information" + "user I.D is "+ user.getUser_id());

                    user.setUsername(documentSnapshot.getString("username"));
                    user.setEmail(documentSnapshot.getString("email"));
                    user.setPhone_number(documentSnapshot.getLong("phone_number"));
                    user.setUser_id(documentSnapshot.getString("user_id"));
                    mUser.add(user);

                }
            }

        }
    });
    return new UserSettings(mUser, mSettings );


}

我尝试从文档中获取这些数据

//setting Profile Widgets
private void setProfileWidgets(UserSettings userSettings){
    Log.d(TAG, "setProfileWidgets: Setting widgets with data retrieved from firebase firestore" + userSettings.toString());
    Log.d(TAG, "setProfileWidgets: searching for username" + userSettings.getSettings().getUsername());


    User user = userSettings.getUser();
    UserAccountSettings settings = userSettings.getSettings();

    try {


    UniversalImageLoader.setSingleImage(settings.getProfile_photo(), mProfilePhoto, null, "");
    mDisplayname.setText(settings.getDisplay_name());
    mUsername.setText(settings.getUsername());
    mWebsite.setText(settings.getWebsite());
    mDescription.setText(settings.getDescription());
    mPosts.setText(String.valueOf(settings.getPosts()));
    mFollowers.setText(String.valueOf(settings.getFollowers()));
    mFollowing.setText(String.valueOf(settings.getFollowing()));
    mProgressBar.setVisibility(View.GONE);
    }
    catch (NullPointerException e){
        Log.e(TAG, "setProfileWidgets: NullPointerException" + e.getMessage());
    }

}

然后我初始化小部件

mFirebaseFirestore = FirebaseFirestore.getInstance();
            myUserRef = mFirebaseFirestore.collection("users");
            myUserSettingsRef = mFirebaseFirestore.collection("user_account_settings");

            myUserSettingsRef.addSnapshotListener(new EventListener<QuerySnapshot>() {
                @Override
                public void onEvent(QuerySnapshot documentSnapshot, FirebaseFirestoreException e) {

                    if (e != null){
                        Log.w(TAG, "onEvent: ", e);
                        return;
                    }
                        for (DocumentSnapshot document : documentSnapshot) {
                            Log.d(TAG, document.getId() + " => " + document.getData());
                            //retrieve user information from the database
                            setProfileWidgets(mFirebaseMethods.getUserSettings(document));
                        }
                }
            });

            myUserRef.addSnapshotListener(new EventListener<QuerySnapshot>() {
                @Override
                public void onEvent(QuerySnapshot documentSnapshot, FirebaseFirestoreException e) {
                    if (e != null){
                        Log.w(TAG, "onEvent: ", e);
                        return;
                    }
                    for (DocumentSnapshot document : documentSnapshot) {
                        Log.d(TAG, document.getId() + " => " + document.getData());

                        //retrieve user information from the database
                        setProfileWidgets(mFirebaseMethods.getUserSettings(document));
                    }
                }
            });

当我运行应用程序时,这就是我得到的

error message

如果你看到代码,我可以看到其他用户信息。但它会在我的小部件上抛出一个空指针异常(user = null,settings = null)。我已经浏览了所有小部件ID并且正确的我想知道是否有人可以指出哪里出错了。提前谢谢

1 个答案:

答案 0 :(得分:-1)

public UserSettings(ArrayList<User> mUser, ArrayList<UserAccountSettings> mSettings){ } 类有一个如下所示的构造函数:

user

此构造函数不设置此类中定义的settingsgetUserSettings变量,它是用于创建return new UserSettings(mUser, mSettings ); // `mUser` and `mSettings` are `ArrayList`s 方法的返回值的构造函数。

Date

因此例外。

这是主要问题,即产生您提到的异常的问题,但这不是唯一的问题。