简体   繁体   English

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

[英]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 = nullloginExist.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.

相关问题 如何在Play框架中更新个人资料记录 - How do I update my profile record in play framework 我想将用户个人资料保存在本地sqlite数据库中,并将用户信息保存 - I want to save user profile in local sqlite database and the user information 为什么我在Eclipse更新站点中看不到新功能? - Why can't I see new features in my Eclipse update site? 如何使用Intent打开用户的个人资料联系人? - How can I open the user's profile contact using an Intent? 当用户在聊天中收到另一个用户的新消息时,如何创建通知? - How can I create a notification when a user receives new message from another user in my chat? 我如何将我的方法中的 output 信息作为一个表发送给用户? - How do I output information from my Method to the user as a table? 如何配置在我的LAN上运行的Java应用程序? - How can I profile a Java application running on my LAN? 如何将大的个人资料图片添加到导航抽屉中? - How can I add a large profile picture to my navigation drawer? 如何使用Geckodriver保留Firefox配置文件的缓存? - How can I retain my Firefox profile's cache with Geckodriver? 如何从后台服务更新Android活动中的信息 - How can I update information in an Android Activity from a background Service
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM