Search code examples
javahibernatespring-bootentitythymeleaf

org.hibernate.PropertyAccessException: Null value was assigned to a property of boolean type


I'm working on java web app, which uses Spring Boot, Hibernate and thymeleaf. At the moment I'm trying to implement registration process for my application and I'm stuck on a problem with my entity class.

Part of User @Entity clas

@Column(name = "aktywny")
private boolean enabled;

@Column(name = "token")
private String confirmationToken;

public boolean getEnabled() {
    return enabled;
}

public void setEnabled(boolean value) {
    this.enabled = value;
}

Request method

@RequestMapping(value = "/register", method = RequestMethod.POST)
public ModelAndView processRegistrationForm(Model model, ModelAndView modelAndView, @Valid User user, BindingResult bindingResult, @RequestParam Map requestParams, RedirectAttributes redir, HttpServletRequest httpServletRequest){

    //Lookup user in db by email
    User userExist = userService.findByEmail(user.getEmail());

    System.out.println(userExist);

    if( userExist != null){
        model.addAttribute("alreadyRegisteredMessage", "Użytkownik o podanym adresie e-mail już istnieje");
        bindingResult.reject("email");
    }

    if(bindingResult.hasErrors()){
        modelAndView.setViewName("home");
    }else {

        //set disabled until confirmation link clicked
        user.setEnabled(false);

        //generate string token
        user.setConfirmationToken(UUID.randomUUID().toString());

        Zxcvbn passwordCheck = new Zxcvbn();

        Strength strength = passwordCheck.measure(requestParams.get("password").toString());

        if(strength.getScore() < 3) {
            bindingResult.reject("password");

            redir.addFlashAttribute("errorMessage", "Twoje hasło jest zbyt słabe, wybierz silniejsze");

            modelAndView.setViewName("redirect: confirm?token=" + requestParams.get("token"));
            System.out.println(requestParams.get("token"));

            // Set new password
            user.setPassword(bCryptPasswordEncoder.encode(requestParams.get("password").toString()));


        }

        userService.saveUser(user);

        String appUrl = httpServletRequest.getScheme() + "://" + httpServletRequest.getServerName();

        SimpleMailMessage registrationEmail = new SimpleMailMessage();
        registrationEmail.setTo(user.getEmail());
        registrationEmail.setSubject("Potwierdzenie rejestracji");
        registrationEmail.setText("Aby dokończyć rejestrację, kliknij w poniższy link: "
                    + appUrl + "/confirm?token=" + user.getConfirmationToken());
        registrationEmail.setFrom("hotelwaltertorun@gmail.com");
        emailService.sendEmail(registrationEmail);

        if (user == null) { // No token found in DB
            modelAndView.addObject("invalidToken", "Oops!  This is an invalid confirmation link.");
        } else { // Token found
            modelAndView.addObject("confirmationToken", user.getConfirmationToken());
        }



        model.addAttribute("confirmationMessage", "E-mail potwierdzający został wysłany na adres " + user.getEmail());
        modelAndView.setViewName("home");
    }

    return modelAndView;

}

HTML form code

<form th:autocomplete="on" id="register_form" class="form-horizontal"  action="#"
                              th:action="@{/register}" th:object="${user}" method="post" role="form"
                              data-toggle="validator">
                            <input type="hidden" name="token" th:value="${confirmationToken}">

                            <div class="col-md-6 form-group">
                                <div class="input-group">
                                    <span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
                                    <input type="text" th:field="*{firstname}"
                                           placeholder="Imię" class="form-control" required/>
                                </div>
                            </div>

                            <div class="col-md-6 form-group">
                                <div class="input-group">
                                    <span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
                                    <input type="text" th:field="*{lastname}"
                                           placeholder="Nazwisko" class="form-control" required/>                                    </div>
                            </div>

                            <div class="col-md-6 form-group">
                                <div class="input-group">
                                    <span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
                                    <input type="text" th:field="*{username}"
                                           placeholder="Login" class="form-control" required/>
                                </div>
                            </div>

                            <div class="col-md-6 form-group">
                                <div class="input-group">
                                    <span class="input-group-addon"><i class="glyphicon glyphicon-lock"></i></span>
                                    <input name="password" type="password" id="password"
                                           placeholder="Hasło" class="form-control" required />
                                </div>
                            </div>

                            <div class="col-md-6 form-group">
                                <div class="input-group">
                                    <span class="input-group-addon"><i class="glyphicon glyphicon-lock"></i></span>
                                    <input type="password" class="form-control" id="signup-password-confirm" placeholder="Potwierdź hasło" name="ConfirmPassword" data-fv-notempty="true"
                                           data-fv-notempty-message="Please confirm password"
                                           data-fv-identical="true"
                                           data-fv-identical-field="password"
                                           data-fv-identical-message="Both passwords must be identical" />
                                </div>
                            </div>

                            <div class="col-md-6 form-group">
                                <div class="input-group">
                                    <span class="input-group-addon"><i class="glyphicon glyphicon-envelope"></i></span>
                                    <input type="email" th:field="*{email}"
                                           placeholder="Adres e-mail" class="form-control"
                                           data-error="This email address is invalid" required />
                                </div>
                            </div>

                            <div class="col-md-6 form-group">
                                <div class="input-group">
                                    <span class="input-group-addon"><i class="glyphicon glyphicon-phone"></i></span>
                                    <input type="tel" th:field="*{phone}"
                                           placeholder="Telefon" class="form-control"
                                           data-error="This email address is invalid" required />
                                </div>
                            </div>

                            <div class="col-md-6 form-group">
                                <button id="register" class="btn btn-success" name="register" style="width:100%;">Zarejestruj&nbsp;&nbsp; <span class="glyphicon glyphicon-send"></span></button>
                            </div>
                        </form>

and error description from browser

There was an unexpected error (type=Internal Server Error, status=500). org.hibernate.PropertyAccessException: Null value was assigned to a property [class com.kaceper.model.User.enabled] of primitive type setter of com.kaceper.model.User.enabled

Thanks for help


Solution

  • Thymeleaf is trying to execute something like this:

    user.setEnabled(null)
    

    Which causes a NullPointerException since enabled is a primitive type and can only be true or false.

    Change the enabled field to Boolean instead of boolean and update the getter and setter accordingly.