我怎么不能用新信息更新我的用户资料?

how can't I update my user profile with new information?

如果 String loginExist = nullloginExist.equals(principal.getName())

我想更新我的用户资料

问题是我收到 NullPointerException。

这是我的代码:

// update profile
@RequestMapping(value = "/updateRH", method = RequestMethod.POST)
public ModelAndView updateRH(Principal principal, @ModelAttribute("user") user user) {
    ModelAndView mv = new ModelAndView();
    String loginExist = "";
    user rh = RhRepo.findByUsername(principal.getName());
    try {
        loginExist = userRepo.findCountUsername(user.getUsername());
    } catch (Exception e) {

    }
    System.out.println(loginExist);

    if (loginExist.equals(null) || loginExist.equals(principal.getName())) {
        user.setId(RhRepo.findByUsername(principal.getName()).getId());
        user.setPwd(encoder.encode(user.getPwd()));
        RhRepo.save(user);
    } else {
        String msg = "Username Deja exist !!!";
        mv.addObject("msg", msg);
    }

    mv.addObject("rh", rh);
    mv.setViewName("rhprofile");
    return mv;
}

loginExist.equals(null) 如果 loginExist 为 null,当您尝试从 null 对象调用方法时,将抛出 NPE。

使用:-

loginExist == null

相反。

所以问题是user.getUsername()user 为 null 并试图获取它的 username 会导致 NullPointerWxception。

在该调用之前添加一个检查,试试这个:

if (user != null) {
    loginExist = userRepo.findCountUsername(user.getUsername());
}

否则(如果为空),您需要先创建用户,然后再尝试从存储库中查找它。