java.lang.NumberFormatException: null

at java.lang.Integer.parseInt(Integer.java:542)
at java.lang.Integer.valueOf(Integer.java:766)
at www.pengzheng.web.LoginServlet.service(LoginServlet.java:33)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:617)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:668)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1521)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1478)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)

代码:   request.setCharacterEncoding("Utf-8");
    response.setContentType("text/html;charset=utf-8");
    PrintWriter out=response.getWriter();
    Integer empno=Integer.valueOf(request.getParameter("name"));
    out.println(empno);
    EmpDao dao=new EmpDaoImpl();
    Emp emp=dao.queryEmpByNo(empno.intValue());
    if(empno.toString()==""||empno==null){
        out.print("empno为空");
        request.setAttribute("error", "用户编号不存在");
        request.getRequestDispatcher("login.jsp").forward(request, response);
    }
    if(emp!=null){
         request.setAttribute("empno", empno);
         response.sendRedirect("ok.jsp");


            jsp代码:

                 <form method="get" action="login">
   <input  type="text" name="empname" /><span>${error!=null?error:""}</span><br/>
   <input type="submit" value="登录"/>
  </form>

8个回答

Finder的GUI占资源太多

类型转换失败吧,你是不是把一个空的字符串转为int类型了,

chengzui
chengzui 我传参了啊
3 年多之前 回复
 在你的Servlet类的33行,解析字符串为integer的时候,参数没有判断空指针。

Integer empno=Integer.valueOf(request.getParameter("name"));
out.println(empno);
你这个里面的request会不会获得空值,如果有可能获得空值,就需要先判断非空,然后再执行你的那段代码

Integer empno=Integer.valueOf(request.getParameter("name"));这行代码有问题吧,打印一下request.getParameter("name")的值

可能得到的参数是空值,所以会报错

先断点确定具体哪行报错,之后看看类型转换为什么说你的参数为null,因为null进行类型转换是会报错的。

谢谢大家,错误已经找到,传参传错了,给传成了参数名

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问