Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I tell request getparameter to treat null value as 0?

So I am currently doing a crud in spring boot and I am stuck at this place where i want to treat null id as 0, I have tried below version and it returns

`java.lang.NumberFormatException: For input string: ""`

   @ResponseBody
    public String checkMobileEmail(HttpServletRequest req, Model model) {
        String mobile = req.getParameter("mobile");
        String email = req.getParameter("email");
        Long id = Long.parseLong(req.getParameter("id"));
        if (req.getParameter("id").equals("")) {
            id  = 0L;
        } 
        System.err.println("id : " + id + " mobile : " + mobile + " email: " + email);
        return service.findByEmailAndMobile(email,mobile,id);
    }

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You must avoid Long.parseLong(req.getParameter("id")); before the if condition as follows:

@ResponseBody
public String checkMobileEmail(HttpServletRequest req, Model model) {
   String mobile = req.getParameter("mobile");
   String email = req.getParameter("email");
   String idParameter = req.getParameter("id");
   Long id = 0L;
   if (idParameter != null && !idParameter.equals("")) {
       id = Long.parseLong(idParameter);
   } 
   System.err.println("id : " + id + " mobile : " + mobile + " email: " + email);
   return service.findByEmailAndMobile(email,mobile,id);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading