当前位置: 代码迷 >> Java Web开发 >> java.lang.NumberFormatException: For input string: ""报错,求大家帮助,该如何处理
  详细解决方案

java.lang.NumberFormatException: For input string: ""报错,求大家帮助,该如何处理

热度:2830   发布时间:2013-02-25 21:21:02.0
java.lang.NumberFormatException: For input string: ""报错,求大家帮助
java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:504)
at java.lang.Integer.parseInt(Integer.java:527)
at org.apache.jsp.cart_005fadd_jsp._jspService(cart_005fadd_jsp.java:68)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at Servlet.SetCharacterEncoding.doFilter(SetCharacterEncoding.java:43)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:729)

代码如下:
  String goodsID=request.getParameter("fruit_id1");
String goodsPrice=request.getParameter("price");
System.out.print(goodsID);
System.out.print(goodsPrice);
int goodsId=Integer.parseInt(goodsID);
float goodsprice=Integer.parseInt(goodsPrice);


------解决方案--------------------------------------------------------
12: int goodsId=Integer.parseInt(goodsID);

?
if ( goodsID == null || "".equals(goodsID.trim())) {
throw new RuntimeException("XXXXX");
}
int goodsId=Integer.parseInt(goodsID);


第13行一样也要修改
------解决方案--------------------------------------------------------
float goodsprice=Integer.parseInt(goodsPrice);


这个有问题吧,
Integer.parseInt(goodsPrice);这句话转化以后是int类型的,你赋值给的goodsprice是float类型,能对吗?


还有你哪个报错貌似是java.lang.NumberFormatException: For input string: ""


这个貌似是 没接收到东西,为空了吧
------解决方案--------------------------------------------------------
在获取页面参数时,做一下判断即可,如下:
String goodsID=request.getParameter("fruit_id1");
String goodsPrice=request.getParameter("price");
int goodsId;
float goodsprice;
if(goodsID==null||goodsID==""){
goodsId=0;
}else{
goodsId=Integer.parseInt(goodsID);
}
if(goodsPrice==null||goodsPrice==""){
goodsprice=0;
}else{
goodsprice=Integer.parseInt(goodsPrice);
}
  相关解决方案