[英]how can't I update my user profile with new information?
如果String loginExist = null
或loginExist.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 為 null,loginExist.equals(null) 將拋出 NPE,因為您嘗試從 null object 調用方法。
利用:-
loginExist == null
反而。
所以問題是user.getUsername()
。 user
是 null 並嘗試獲取其username
會導致 NullPointerWxception。
在該調用之前添加一個檢查,試試這個:
if (user != null) {
loginExist = userRepo.findCountUsername(user.getUsername());
}
否則(如果它為空),您需要在嘗試從存儲庫中找到它之前創建用戶。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.