导致NumberFormatException的Servlet代码

时间:2017-03-25 14:40:12

标签: java jsp servlets

对于使用Java,JSP和MySQL的在线基于表单的预订系统,我的servlet代码不断在“int customer”开头的行中抛出NumberFormatException。除了这里,我的代码没有别的问题。

  

java.lang.NumberFormatException:对于输入字符串:“null”

如果我检查空字符串或空字符串,我得到的只是错误。

if (submit.equals("add booking")) {        
                int customerID = Integer.parseInt(request.getParameter("customerID").trim());
                int routeID = Integer.parseInt(request.getParameter("routeID").trim());
                String sailing = request.getParameter("sailing").trim();
                int noAdults = Integer.parseInt(request.getParameter("noAdults").trim());
                int noChildren = Integer.parseInt(request.getParameter("noChildren").trim());
                String otherinfo = request.getParameter("otherinfo").trim();
                if (sailing.equalsIgnoreCase("")) {
                    url = "/scotiavalidation.jsp";
                } else {
                    bookingManager.addBooking(customerID, routeID, sailing, noAdults, noChildren, otherinfo);
                    int bookingID = 0;
                    bookingID = bookingManager.getBookingIdByAdd(bookingID);
                    request.setAttribute("bookingID", bookingID);
                    url = "/bookingConfirmation.jsp";
                }

我知道我需要在某处尝试捕获块,但我无法弄明白。谁有人建议解决方案? 我的Glassfish 4.1.1输出如下所示:

Warning:   StandardWrapperValve[BookingServlet]: Servlet.service() for servlet BookingServlet threw exceptionjava.lang.NumberFormatException: For input string: "null"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
at controller.BookingServlet.processRequest(BookingServlet.java:46)
at controller.BookingServlet.doGet(BookingServlet.java:161)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:416)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:283)
at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:206)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:180)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:283)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:200)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:132)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:111)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:536)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:591)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:571)
at java.lang.Thread.run(Thread.java:745)

我正在使用My JSP页面中的会话。这是一个片段:

<form name="addBookingForm" action="BookingServlet">
                <table cellspacing="8" border="0">
                    <tr>
                        <td align="left"><p><label for="customerID">Customer No:</label></p></td>
                        <td><p><%= session.getAttribute("customerID")%></p></td>
                    </tr>
                    <tr>
                        <td align="left"><p><label for="routeID">Cruise No:</label></p></td>
                        <td><select name="routeID" id="routeID" value="">
                                <option selected></option>
                                <option>322</option>
                                <option>323</option>
                                <option>324</option>
                            </select></td>
                    </tr>

3 个答案:

答案 0 :(得分:1)

Integer.parseInt(value)。此处的值可能无法解析为int。它可能有字母或其他字符。最好先验证用户输入,以便在逐步调整到int之前检查它是否为数值。

您可以使用Apache Commons Lang 3.5 NumberUtils.isCreatable或StringUtils.isNumeric。

进行验证

答案 1 :(得分:1)

似乎您对customerID的值以字符串“null”返回。您需要在投射之前进行验证。您还应该检查JSP为什么将customerID设置为“null”。您还需要检查您的servlet。像

这样的东西
if (submit.equals("add booking")&&request.getParameter("customerID")!=null)

答案 2 :(得分:1)

错误消息很明确:

  

java.lang.NumberFormatException:对于输入字符串:“null”

您正在将null值解析为数字。你确定填充了整数参数吗?。

在致电nullInteger.parse之前检查trim指针。