NumberFormatException:JSP中的null

时间:2014-07-13 07:00:31

标签: java jsp servlets model-view-controller jdbc

我有一个表单,用户可以在其中创建包含信息的事件(例如,事件ID,事件名称。事件位置...)

然后将事件信息存储在数据库中(使用jsp for form,java beans getter和setter,jdbc sql)。

我想检索所有事件信息并将其显示在另一个jsp页面上,但我收到numberformatexception null错误。

我运行了一个测试java文件,它可以正确获取所有事件信息而没有错误,但是当我尝试将其显示在表格内的jsp页面上时,我会收到错误。

当我尝试将以下网址放在地址栏中时

http://localhost:8080/Assignment/addEvent?action=list 

我得到数字格式,除了空错误。

WebServlet url模式是“/ addEvent”。

以下是我的相关代码。

EventController Servlet的代码

    String action = request.getParameter("action");



          if ("list".equalsIgnoreCase(action)) {
        // call the query db to get retrieve for all customer 
        init();
        ArrayList events = db.queryEvents();
        // set the result into the attribute

        request.setAttribute("events", events);
        // redirect the result to the listCustomers.jsp
        RequestDispatcher rd;
        rd = getServletContext().getRequestDispatcher("/AllEventsDisplay.jsp");
        rd.forward(request, response);
    }
    else {
        PrintWriter out = response.getWriter();
        out.println("Creating event failed!!");
    }

AllEventsDisplay.jsp页面

<%
                 ArrayList<EventBean> events = (ArrayList<EventBean> )request.getAttribute("events");
                out.println("<h1>Events</h1>");



                out.println("<table border='1'>");
                out.println("<tr>");
                out.println("<th>Event ID</th>  <th> Event Name</th><th> Event Location</th><th> Event Venue</th ><th> Event Date</th ><th> Event Helpers</th ><th> Helper Type</th >");
                out.println("</tr>");
  // loop through the customer array to display each customer record
                for (int i = 0; i < events.size(); i++) {
                    EventBean c = events.get(i);
                    out.println("<tr>");

                    out.println("<td>" + c.getEventID() + "</td>");
                    out.println("<td>" + c.getEventname() + "</td>");
                    out.println("<td>" + c.getLocation() + "</td>");
                    out.println("<td>" + c.getVenue() + "</td>");
                    out.println("<td>" + c.getDate() + "</td>");
                    out.println("<td>" + c.getNumofhelper() + "</td>");
                    out.println("<td>" + c.getHelpertype() + "</td>");


                    out.println("</tr>");

                }
                out.println("</table>");
    %>

获取事件信息的EventDB方法

        public ArrayList queryEvents() {
    Connection cnnct = null;
    PreparedStatement pStmnt = null;
    EventBean cb = null;
    ArrayList ca = new ArrayList();
    try {
        cnnct = getConnection();
        String preQueryStatement = "SELECT * FROM EventInfo";
        pStmnt = cnnct.prepareStatement(preQueryStatement);
        ResultSet rs = null;
        rs = pStmnt.executeQuery();
        while (rs.next()) {
            cb = new EventBean();
            cb.setEventID(rs.getString(1));
            cb.setEventname(rs.getString(2));
            cb.setLocation(rs.getString(3));
            cb.setVenue(rs.getString(4));
            cb.setDate(rs.getString(5));
            cb.setNumofhelper(rs.getInt(6));
            cb.setHelpertype(rs.getString(7));
            ca.add(cb);
        }
    } catch (SQLException ex) {
        while (ex != null) {
            ex.printStackTrace();
            ex = ex.getNextException();
        }
    } catch (IOException ex) {
        while (ex != null) {
            ex.printStackTrace();

        }
    }

    return ca;

   }

请注意NumofHelper是一个整数。

错误堆栈跟踪

WARNING:StandardWrapperValve[EventController]: Servlet.service() for servlet 
                         EventController threw exception

java.lang.NumberFormatExcept ion: null
at java.lang.Integer.parseInt(Integer.java:454)
at java.lang.Integer.parseInt(Integer.java:527)
at ict.servlet.EventController.processRequest(EventController.java:65)
at ict.servlet.EventController.doGet(EventController.java:45)
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:357)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
at java.lang.Thread.run(Thread.java:724)

信息:访问未经访问的参考文献 信息:访问未经访问的参考文献 信息:访问未经访问的参考文献 信息:在[/ Assignment]加载申请[作业] 信息:分配已在322毫秒内成功部署。

1 个答案:

答案 0 :(得分:0)

在EventController.java中,EventHelpers值为空值。您没有检查值是否为null,您尝试解析“null”值。所以它给出了NullPointerException。

 int NumberofHelper = 0;
 if(EventHelpers != null){
    NumberofHelper  = Integer.parseInt(EventHelpers);
}

试试这个