org.hibernate.PropertyAccessException:将空值赋给布尔类型的属性

时间:2017-12-07 21:07:01

标签: java hibernate spring-boot entity thymeleaf

我正在研究使用Spring Boot,Hibernate和thymeleaf的java web应用程序。目前,我正在尝试为我的应用程序实施注册过程,而且我遇到了实体类的问题。

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

请求方法

@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 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>

来自浏览器的错误说明

  

出现意外错误(type = Internal Server Error,status = 500)。   org.hibernate.PropertyAccessException:将空值赋给com.kaceper.model.User.enabled的原始类型setter的属性[class com.kaceper.model.User.enabled]

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

Thymeleaf正试图执行这样的事情:

user.setEnabled(null)

导致NullPointerException。将enabled字段更改为Boolean而不是boolean,并相应地更新getter和setter。