[英]how can't I update my user profile with new information?
I want to update my user profile if the String loginExist = null
or loginExist.equals(principal.getName())
如果String loginExist = null
或loginExist.equals(principal.getName())
我想更新我的用户配置文件
the problem is that I get NullPointerException.问题是我得到 NullPointerException。
this is my code:这是我的代码:
// 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) will throw NPE if loginExist is null as you are trying to call a method from a null object.如果 loginExist 为 null,loginExist.equals(null) 将抛出 NPE,因为您尝试从 null object 调用方法。
Use:-利用:-
loginExist == null loginExist == null
instead.反而。
So the problem is user.getUsername()
.所以问题是user.getUsername()
。 the user
is null and trying to get its username
causes a NullPointerWxception. user
是 null 并尝试获取其username
会导致 NullPointerWxception。
add a check before that call, try this:在该调用之前添加一个检查,试试这个:
if (user != null) {
loginExist = userRepo.findCountUsername(user.getUsername());
}
otherwise (if it is null), you need to create the user before trying to find it from repository.否则(如果它为空),您需要在尝试从存储库中找到它之前创建用户。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.