这是JSP页面:
<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
    
 <%@ page import="com.hello.DAO.*, com.hello.userImpl.* , com.hello.User.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head><%
        request.setCharacterEncoding("GBK");
        
         UserDAO UD = new UserDAOImpl();
         int id = 0;
         if(request.getParameter("id")!=null && request.getParameter("id")!=""){
             id = Integer.parseInt(request.getParameter("id"));
         }
         else{
            out.println("id不能为空!");
          // return;
         }
         User u = UD.loadById(id);
       
        String action = request.getParameter("action");
        if(action != null && action.trim().equals("updateUser")){
                  String name = request.getParameter("name");
                  String password = request.getParameter("password");
                 User user = new User();
                 user.setId(id);
                  user.setName(name);
                  user.setPassword(password);
                  UD.updateUser(user);
                  out.println("更新成功!");
                
             } %><body>
     <form action="updateUser.jsp" method="post">
       <input type="hidden" name = "action" value = "updateUser"/>
       <input type="hidden" name="action" value="<%=id %>"/>
       
       <input type= "text" name="name" value="<%=u.getName()%>" ><br>
       <input type= "password" name="password" value="<%=u.getPassword()%>" ><br>
         <input type= "submit" name="submit" value="修改" >
     </form>
      <a href="index.jsp">返回首页</a>
</body>
</html>
所报的错信息如下:
     java.lang.NullPointerException
at org.apache.jsp.updateUser_jsp._jspService(updateUser_jsp.java:104)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:337)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Unknown Source)
  

解决方案 »

  1.   

    if(action != null && action.trim().equals("updateUser"))改成if (action != null && ("updateUser").equals(action.trim())) 看看
      

  2.   

    上面说的一定是那个u为null
      

  3.   

    User u = UD.loadById(id); 
    这句没得到对象 是id或者loadById()的问题 通常是id没得到想要 的值
      

  4.   

    <%@ page language="java" contentType="text/html; charset=GB18030" 
        pageEncoding="GB18030"%> 
        
    <%@ page import="com.hello.DAO.*, com.hello.userImpl.* , com.hello.User.*" %> 
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
    <html> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=GB18030"> 
    <title>Insert title here </title> 
    </head> 
    <% 
            request.setCharacterEncoding("GBK"); 
            UserDAO UD = new UserDAOImpl(); 
            String id = request.getParameter("id");
            User u = null;
            if(id !=null && id!=""){ 
                 u = UD.loadById(Integer.parseInt(id)); 
            } 
            else{ 
                out.println("id不能为空!"); 
                response.sendRedirect(" 返回到你的前一个页面");
            } 
            String action = request.getParameter("action"); 
            if(action != null && action.trim().equals("updateUser")){ 
                      String name = request.getParameter("name"); 
                      String password = request.getParameter("password"); 
                      User user = new User(); 
                      user.setId(id); 
                      user.setName(name); 
                      user.setPassword(password); 
                      UD.updateUser(user); 
                      out.println("更新成功!"); 
                } 
    %> <body> 
        <form action="updateUser.jsp" method="post"> 
          <input type="hidden" name = "action" value = "updateUser"/> 
          <input type="hidden" name="action" value="<%=id %>"/> 
          
          <input type= "text" name="name" value="<%=u.getName()%>" > <br> 
          <input type= "password" name="password" value=" <%=u.getPassword()%>" > <br> 
          <input type= "submit" name="submit" value="修改" > 
        </form> 
          <a href="index.jsp">返回首页 </a> 
    </body> 
    </html> 
      

  5.   

    试了下,还是报以前的错.
    HTTP Status 500 - --------------------------------------------------------------------------------type Exception reportmessage description The server encountered an internal error () that prevented it from fulfilling this request.exception org.apache.jasper.JasperException: An exception occurred processing JSP page /updateUser.jsp at line 4542:        <input type="hidden" name = "action" value = "updateUser"/>
    43:       <input type="hidden" name="action" value="<%=id %>"/>
    44:        
    45:        <input type= "text" name="name" value="<%=u.getName()%>" ><br>
    46:        <input type= "password" name="password" value="<%=u.getPassword()%>" ><br>
    47:          <input type= "submit" name="submit" value="修改" >
    48:      </form>
    Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:505)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:416)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:337)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    root cause java.lang.NullPointerException
    org.apache.jsp.updateUser_jsp._jspService(updateUser_jsp.java:103)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:337)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    note The full stack trace of the root cause is available in the Apache Tomcat/6.0.16 logs.烦死了,折腾我好几天了.
      

  6.   

          wd422988145的方法中。。UD.loadById(Integer.parseInt(id));传入的ID同样没有获得值的时候,u始终为空,那么你在下面
    <input type= "text" name="name" value=" <%=u.getName()%>" > <br> 
    <input type= "password" name="password" value=" <%=u.getPassword()%>" > <br> 使用get方法的时候,还是会报空指针异常,那么这里,还是需要判断对象u是否为空呢
    <input type= "text" name="name" value=" <%=u!=null?u.getName(): "" %>" > <br> 
    <input type= "password" name="password" value=" <%= u!=null? u.getPassword() : ""%>" > <br> 这里使用三元运算,再判断一次,如果为u为null,值name,和password都为" "空字符串,或者你定义其他的值,作为后台判断依据~
      

  7.   

     u = UD.loadById(Integer.parseInt(id)); 为空没做检查
    打印出来看看是否存在值
      

  8.   

    不知道你的页面叫什么,出现了空指针,看一下你的这个jsp页面编译好之后的.java,看看哪行出错了。
    org.apache.jsp.updateUser_jsp._jspService(updateUser_jsp.java:104)