【问题标题】:HTTP Status 500 - java.lang.NumberFormatException: nullHTTP 状态 500 - java.lang.NumberFormatException: null
【发布时间】:2013-02-06 08:48:24
【问题描述】:

我正在创建一个标准的动态 Web 项目,包括一个 Servlet、一个过滤器来过滤请求和 jsp 页面。 我从welcome.jsp 中获取书籍的数量,并根据数量填充add.jsp 上的输入框。我想过滤来自 add.jsp 的数据。当我通过正则表达式验证数据时,它向我显示错误。我的流程也是正确的,即从开始 FILTER --> CONTROLLER --> FILTER --> PAGE 等等???

错误: java.lang.NumberFormatException: null

完整的跟踪是这样的:


HTTP Status 500 - java.lang.NumberFormatException: null

type Exception report

message java.lang.NumberFormatException: null

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

exception

org.apache.jasper.JasperException: java.lang.NumberFormatException: null
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:549)
    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:728)
    com.filter.RequestFilter.doFilter(RequestFilter.java:107)

root cause

java.lang.NumberFormatException: null
    java.lang.Integer.parseInt(Unknown Source)
    java.lang.Integer.parseInt(Unknown Source)
    org.apache.jsp.WEB_002dINF.add_jsp._jspService(add_jsp.java:139)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    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:728)
    com.filter.RequestFilter.doFilter(RequestFilter.java:107)

显示的代码是:

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    // TODO Auto-generated method stub
    // place your code here
    boolean flag = true;
    HttpServletRequest httpServletRequest = (HttpServletRequest) request;
    HttpServletResponse httpResponse = (HttpServletResponse) response;
    httpServletRequest.getServletPath();
    System.out.println(httpServletRequest.getServletPath());


    String address = InetAddress.getLocalHost().getHostAddress();
    System.out.println(address); 


    // For the first time (Welcome page) i will be 0 so that it will perform first if

    if(i == 0)
    {
        System.out.println(i);
        if(verify(address))
        {
            chain.doFilter(request, response);
            i++;
        }

        else
        {
            RequestDispatcher requestDispatcher = httpServletRequest.getServletContext().getRequestDispatcher("/WEB-INF/forbidden.jsp");
            requestDispatcher.forward(request, response);
            return;
        }

    }

    // For the second time (Controller) i will be 1 so that it will perform first if


    else if(i == 1)
    {
        System.out.println(i);
        i++;
        System.out.println("after ++"+i);
        chain.doFilter(request, response);
    }

    // For the second time (add page) i will be 2 so that it will perform first if


    else if(i == 2)
    {

        if(specialChara(httpServletRequest))
            {
                System.out.println(i);
                System.out.println("done");
                i++;
                chain.doFilter(request, response);
            }

            else
            {
                System.out.println(i);
                flag = false;
                System.out.println(""+httpServletRequest.toString()+ "\n"+httpResponse.toString());
                RequestDispatcher requestDispatcher = httpServletRequest.getServletContext().getRequestDispatcher("/WEB-INF/add.jsp");
                requestDispatcher.forward(request, response);

            }
    }

    // For the third time (Controller) i will be 2 so that it will perform first if

    else if(i == 3)
    {
        System.out.println(i);
        i++;
        chain.doFilter(request, response);
        i = 0;
    }

它在 i==2 的循环中的 forward() 行上给出错误。 可能是什么问题?

add.jsp 的链接是https://docs.google.com/document/d/1fOzymYvlLXS577DrSrznRoeBnI7_hMmuqzFoK02xKoU/edit?usp=sharing

【问题讨论】:

    标签: jsp servlets nullpointerexception servlet-filters


    【解决方案1】:

    从堆栈跟踪看来,问题不在于您的 java 类,而在于您的 JSP。我的猜测是在行

    int no = Integer.parseInt(request.getParameter("no"));
    

    no 似乎为空。此外,您最好使用其他条件标记而不是在 JSP 中使用 scriptlet。

    【讨论】:

    • 你能告诉我应该怎么做吗?我应该将它存储到会话对象中吗??
    • 你应该调试一下为什么“no”是空的。您是否在 Java 类的请求范围内设置此值?
    猜你喜欢
    • 1970-01-01
    • 2011-01-01
    • 2014-01-08
    • 2012-12-04
    • 2017-11-01
    • 2016-05-29
    • 2015-05-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多