Chapter 2: Update User Preferences - null pointer exception

Hello - writing here because I cannot seem to be able to figure this one out.

I am attempting the ticket for updating user preference from Ch2. In every test, after I call the updateUserPreferences method this does not seem to persist and the second part of the unit tests fails because user.getPreferences() returns null.

Here is my implementation of the method in UserDao :

public boolean updateUserPreferences(String email, Map<String, ?> userPreferences) {
        if (userPreferences == null ) {
            return false;
        }"update user prefs - " + userPreferences);
        User user = this.getUser(email);;
        if (user == null){
            return false;
        user.setPreferences((Map<String, String>) userPreferences);;;"favorite_club"));
        return true;

please ignore all the log lines - added those trying to understand what is going on. thank you :slight_smile:

this.getUser(email) will return user preferences in it, but you need to have correctly coded it before. yet it will return null for preferences if none was set before.

then there 2 places a user’s preferences are stored: in memory of app and database. user.setPreferences and user.getPreferences are app part in user model, and this updateUserPreferences should connect to database in DAO.

if no prefs exist on the database then none will on app either. for this, you need to find the user in the database and then execute an updateOne operation into the database with it along with the setPreferences in the app if needed. and for sure your code misses the database update.