-6

This is what my Firestore data structure looks like

Hello, i have an issue and i have searched online but nothing seem to come close to what am facing. i am using firestore to store my data

This is the users node. user section

And this is the user_account_settings node. user_account_settings section

The goal is to loop through these nodes and retrieve/get data from the node and set them into text and display them using custom models.

this is the model for Account settings

public class 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 + '\'' +
            '}';
}

}

and user model

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 settings (Holding both user and 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 +
            '}';
}

}

trying to retrieve and set the data from firestore to the models

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 );


}

and i try getting these data from the documents

//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());
    }

}

then i initialize the widgets

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));
                    }
                }
            });

When i run the app, this is what i get

error message

if you see the code, i can see other user information. but it throws me a null pointer exception on my widgets (user=null , settings=null). i have gone through all the widgets id's and its correct am wondering if someone can point out where am getting wrong. thanks in advance

Seraware
  • 53
  • 2
  • 9

1 Answers1

-2

The UserSettings class has a constructor that looks like this:

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

}

This constructor doesn't set the user and settings variables defined in this class and it is the constructor you use to create the return value of the getUserSettings method.

return new UserSettings(mUser, mSettings ); // `mUser` and `mSettings` are `ArrayList`s

Hence the exception.

This is the main problem, the one that generates the exception you've mentioned but it is not the only problem.

Titus
  • 22,031
  • 1
  • 23
  • 33
  • An explanation for the down vote will be helpful. – Titus Jan 27 '18 at 10:55
  • I didn't downvote you. am still not allowed to vote on post. someone must have done that i truly apologize. if you don't mind, could you point out where my errors are? so i can research them and fix. possibly a solution if am not asking too much. thanks in advance and for quick reply. – Seraware Jan 27 '18 at 11:09