Search code examples
javasqlpostgresqljspnumberformatexception

Error when trying to search similar images using JSP


I am building a web application for feature extraction and comparing the feature of images, using JSP, Java, and PostgreSQL.

My feature extraction is extracting some features from images, so that then I can compare them (some features are about color, some on the histogram of the image and so. I have special methods that extract them using specific libraries (jars that I add into lib) as you can see in the following example!

Those features are then successfully being added into my PostgreSQL database.

public void GetImageProperties(String targetPath, ImageProperties imagesProperties) throws Exception
{
    // load image
    FileInputStream imageStream;
    imageStream = new FileInputStream(targetPath);
    BufferedImage bimg = ImageIO.read(imageStream);        

    // get properties
    imagesProperties.cl = new ColorLayoutImpl(bimg);
    imagesProperties.eh = new EdgeHistogramImplementation(bimg);
    imagesProperties.sc = new ScalableColorImpl(bimg);
}

Then I add them into a postgresql database(only the features, not the images)

String query = "CREATE TABLE Images (" +
               "  imageid SERIAL PRIMARY KEY," +
               "  fname VARCHAR(128)," +
               "  colorlayout VARCHAR(512), " +
               "  edgehist VARCHAR(512), " +
               "  scalablecolor VARCHAR(512) " +
               ")";

I get this error every time I had loaded an image and want to compare it with all the other images in a directory.

HTTP Status 500 -     
type Exception report

message:

description The server encountered an internal error () that prevented it from fulfilling this request.

exception:

org.apache.jasper.JasperException: An exception occurred processing JSP page /query.jsp at line 124

121:  if (bSearch) { 
122:    // get form params 
123:    query.setTopK(new Integer(request.getParameter("frm_topk"))); 
124:    query.setColorHistogramWeight(new Integer(request.getParameter("frm_weight_c1"))); 
125:    query.setColorDistributionWeight(new Integer(request.getParameter("frm_weight_c2"))); 
126:    query.setTextureWeight(new Integer(request.getParameter("frm_weight_c3"))); 
127:  }

Stacktrace:

org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:470)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)    
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)   
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)    
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)

root cause:

java.lang.NumberFormatException: For input string: "1.0"
java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)   
java.lang.Integer.parseInt(Integer.java:492)    java.lang.Integer.<init>
(Integer.java:677)  org.apache.jsp.query_jsp._jspService(query_jsp.java:256)    
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)  
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)    
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)     
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)    
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)   
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)    
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)

note The full stack trace of the root cause is available in the Apache Tomcat/7.0.27 logs.

Solution

  • Your problem is here

    query.setTopK(new Integer(request.getParameter("frm_topk")));
    

    should be

    String parFrm_topk = request.getParameter("frm_topk");
    Integer frm_topk = null;
    if (parFrm_topk != null && parFrm_topk.length() > 0 && !parFrm_topk.equals("null"))
    try {
      frm_topk = Integer.valueOf(parFrm_topk);
    } catch (NumberFormatException nfe){
      throw nfe;
    }
    if (frm_topk ! = null) {
       query.setTopK(frm_topk);
    

    this code is trying to check the parameter that could be empty, why it is empty depends on how flow the app and how parameters are passed through url.