NumberFormatException:使用request.getParameter()从表单获取值时为Null

时间:2019-07-07 19:11:46

标签: java hibernate jsp servlets

我试图创建一个应用程序,在其中提交表单并使用cartnow.java中的request.getparameter()获取表单值。之后,我将所有变量存储在列表中,并使用req.setAttribute在jsp中显示这些变量

Pojo:

public class Products {

    private int pid;
    private String productname;
    private double price;
    private int quantity;    
    private int p;
    private String pn;
    private double pr;
    private int qua;
    private int r;

    // getters and setters omitted for brevity.
}

cart.jsp:

<c:forEach items="${names}" var="name"> 
  <form action="carting"> 
   <tr>
    <td><input type="text" name="Pid" value="${name.pid}" disabled></td>
    <td><input type="text" name="Pname" value="${name.productname}"    disabled></td>
    <td><input type="text" name="Price" value="${name.price}" disabled></td>  
    <td><input type="text" name="Quantity" value="${name.quantity}" disabled></td>
    <td><input type="text" name="req"></td>
<td> <input type="submit" value = "BUY"> </td>
 </tr>
 </form>
</c:forEach>

Cartnow.java:

int p = Integer.valueOf(request.getParameter("Pid"));    
    String pn=request.getParameter("Pname");
    double pr=Double.valueOf(request.getParameter("Price"));
    int qua=Integer.parseInt(request.getParameter("Quantity"));
    int r=Integer.valueOf(request.getParameter("req"));

Products pp=new Products(); 
pp.setP(p); 
pp.setPn(pn); 
pp.setPr(pr);
pp.setQua(qua); 
pp.setR(r);

     List<Products> prod = new ArrayList<Products>();
        prod.add(pp);
    request.setAttribute("cartrow", prod);
RequestDispatcher rs=request.getRequestDispatcher("BuyCart.jsp");
        rs.forward(request, response);

现在,在运行项目时,我收到NumberFormatException:Null

java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.valueOf(Unknown Source)
at common.Cartnow.doGet(Cartnow.java:31)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:200)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:678)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:836)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1839)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)

0 个答案:

没有答案