0

here is the code

if(var!=null) {
                        int varInt= Integer.parseInt(var);
                        method.setProp(varInt);
              }

After some reseach it normally throw error when we're not testing for null, but I am, so what's the problem?

The Error

org.apache.jasper.JasperException: An exception occurred processing JSP page /TravelSearch.jsp at line 81               
80:                    if(var!=null) {
81:                         int varInt= Integer.parseInt(var);
82:                         method.setProp(varInt);
83:                     }
84:                     
Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:567)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:469)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
cause mère
java.lang.NumberFormatException: For input string: ""
Lola Loulita
  • 471
  • 4
  • 21

2 Answers2

0

Take a look at this answer by Jon Skeet.

https://stackoverflow.com/a/1486082/891261

This is what you want - a method that checks if the string can be parsed to a number and returns an obvious illegal value when it does not, which you then can check for and act on.

Community
  • 1
  • 1
Jesper Bangsholt
  • 544
  • 4
  • 11
-1

The number you're trying to parse isn't null, it's an empty string "". The stacktrace was telling you this here:

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

Change your test to do this:

if(var!=null && !"".equals(var)) {
tddmonkey
  • 20,798
  • 10
  • 58
  • 67