检查帐户是否已存在时,空指针异常-Spring + Thymeleaf

时间:2018-07-27 21:03:56

标签: java spring hibernate thymeleaf

我是程序设计的初学者,所以我为这个问题感到抱歉,但是坐在上面几天却仍然不知道。我正在制作博客,希望在注册新用户时检查该帐户是否不存在(检查登录名)。 当我尝试在网站上注册(甚至数据库中没有现有的登录名)时,收到此错误:

access()

看来,UserService的名为“ registerNewUserAccount”的方法存在问题。也许这是一个非常简单的问题,但我找不到。 我试图实例化UserDto(我正在使用数据传输对象)和UserEntity对象,但是发生了编译时错误。 您能帮我找出这段代码有什么问题吗?

有SigninController:

2018-07-27 22:27:03.561 ERROR 19368 --- [nio-8084-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause

java.lang.NullPointerException: null
    at com.jzych.blog_springboot.controllers.SigninController.createUserAccount(SigninController.java:78) ~[classes/:na]
    at com.jzych.blog_springboot.controllers.SigninController.registerUserAccount(SigninController.java:61) ~[classes/:na]

UserService类

@Controller
public class SigninController {

    @Autowired
    UserRepo userRepo;
    UserService service;


    @GetMapping("/register")
    public String registerForm(Model model, WebRequest request){
        UserDto accountDto = new UserDto();
        model.addAttribute("userentity", accountDto);
        return "signinPage";
    }

    @PostMapping("/register")
    public ModelAndView registerUserAccount(@ModelAttribute("userentity") @Valid UserDto accountDto,
                                            BindingResult result, WebRequest request, Errors errors){

        UserEntity registered = new UserEntity();
        if(!result.hasErrors()){
            registered = createUserAccount(accountDto, result); //java:61
        }
        if(registered == null){
            result.rejectValue("login", "message.regError");
        }
        if(result.hasErrors()){
            return  new ModelAndView("signinPage", "userentity", accountDto);
        }
        else{
            return new ModelAndView("homePage", "userentity", accountDto);
        }
    }

    private UserEntity createUserAccount(@Valid UserDto accountDto, BindingResult result) {
        UserEntity registered = null;
        try{
            registered = service.registerNewUserAccount(accountDto); //java:78
        }catch (LoginExistsException e) {
            return null;
        }
        return registered;
    }
}

界面

@Service
public class UserService implements IUserService {

    @Autowired
    private UserRepo userRepo;

    @Override
    public UserEntity registerNewUserAccount(UserDto accountDto)
        throws LoginExistsException
    {

        if(loginExist(accountDto.getLogin())){
            throw new LoginExistsException(
                    "There is an account with that login:" + accountDto.getLogin());
        }

        UserEntity userEntity = new UserEntity();

        userEntity.setName(accountDto.getName());
        userEntity.setLogin(accountDto.getLogin());
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        userEntity.setPassword(passwordEncoder.encode(accountDto.getPassword()));
        userEntity.setRole(accountDto.getRole());

        return userRepo.save(userEntity);
}

    private boolean loginExist(String login){
        UserEntity userEntity = userRepo.getByLogin(login);
                if(userEntity != null) {
                    return true;
                }
                return false;
    }
}

和我的模型

public interface IUserService {
    UserEntity registerNewUserAccount(UserDto accountDto)
        throws LoginExistsException;

UserDto模型

@Entity
@Table(name = "user")
public class UserEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long idUser;

    private String name;

    private String login;

    private String password;

    private String role;

    @OneToMany//(mappedBy = "user") kaskadowość przy usuwaniu?
    private List<Post> posts = new ArrayList<>();

    public UserEntity() {
    }

    public UserEntity(String name, String login, String password, String role) {
        this.name = name;
        this.login = login;
        this.password = password;
        this.role = role;
    }
//getters, setters, toString, hashcode and equals

带有Thymeleaf的HTML中的非常原始的页面,用于注册新用户

public class UserDto {

    @NotNull
    @NotEmpty
    private String name;

    @NotEmpty
    @NotNull
    private String login;

    @NotEmpty
    @NotNull
    private String password;

    private String role;

//getters, setters and toString

1 个答案:

答案 0 :(得分:1)

您忘记了@Autowired UserService类

错误的方式:

@Autowired
UserRepo userRepo;
UserService service;

正确的方法:

@Autowired
UserRepo userRepo;
@Autowired
UserService service;