0

This is my controller which gets the id from the url :

@RequestMapping(value = "/user/users", method = RequestMethod.GET, params = {"getId"})
@ResponseBody
public ModelAndView getClientStat(@RequestParam(value="getId", required = true) String getId) {
    ModelAndView modelAndView = new ModelAndView();

    long userId=0;
    userId=Long.parseLong(getId);
    Client c=userService.getUserById(userId);

    modelAndView.addObject("userName", c.getName() );

    modelAndView.setViewName("user/users");
    return modelAndView;
}

This returns a null pointer exception but the thing is when I just put getID in the modelAndView, like this :

    modelAndView.addObject("id",getId)

It does add the responding id. How can I fix this ? I think the problem is when parsing the string,cuz when that part is removed, it works well.

1 Answers1

0

parseLong cannot return null - It either returns a long value or throws an Exception. So your problem is most likely not string parsing.

Check in which line the NullPointerException happens. I would expect that either userService is null, or the returned Client c is null. I guess it's probably c that is null - Maybe you got an ID that your user service did not find, and it returned null as a result...

David Tanzer
  • 2,732
  • 18
  • 30
  • It is actually the userService that returns a null. I apologize. But in userService I have : public User getUserById(long id); whose implementation is : public Client getUserById(long id) { return userRepository.findOne(id); } So I can't seem to figure out why it doesn't find the user –  Sep 27 '17 at 12:17
  • No need to apologize - I'm glad I could help (at least a little - I hope) ;) But without further info, I cannot answer why your repository does not find a user :( Also, I think it's not the scope of this question anymore. – David Tanzer Sep 27 '17 at 12:45
  • I understand. Thank you! :) –  Sep 27 '17 at 12:58