快捷导航

页面没反应java.lang.NullPointerException

My JSP 'userinfo.jsp' starting page                    .title{   width: 30%;    background-color: #CCC;   font-weight: bold;  }  .content{      width:70%;      background-color: #CBCFE5;  }                 用户信息

    <hr>                 
用户名:
密码:
性别:
E-mail:
出生日期:                                    
爱好:                                                      
自我介绍:
是否介绍协议:
      package servlet;import java.io.IOException;import java.text.SimpleDateFormat;import java.util.Date;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import entity.Users;public class RegServlet extends HttpServlet { /**   * Constructor of the object.   */ public RegServlet() {  super(); } /**   * Destruction of the servlet.
   */ public void destroy() {  super.destroy(); // Just puts "destroy" string in log  // Put your code here } /**   * The doGet method of the servlet.
   *   * This method is called when a form has its tag value method equals to get.   *    * @param request the request send by the client to the server   * @param response the response send by the server to the client   * @throws ServletException if an error occurred   * @throws IOException if an error occurred   */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  doPost(request, response);   } /**   * The doPost method of the servlet.
   *   * This method is called when a form has its tag value method equals to post.   *    * @param request the request send by the client to the server   * @param response the response send by the server to the client   * @throws ServletException if an error occurred   * @throws IOException if an error occurred   */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  request.setCharacterEncoding("utf-8");    Users u = new Users();  String username;  String mypassword;  String gender;  String email;  String introduce;  String isAccept;  Date birthday;  String[] favorites;    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");    try {   username = request.getParameter("username");   mypassword = request.getParameter("mypassword");   gender = request.getParameter("gender");   email = request.getParameter("email");   introduce = request.getParameter("introduce");   if (request.getParameterValues("isAccept")!=null) {    isAccept = request.getParameter("isAccept");   }else {    isAccept = "false";   }   birthday = sdf.parse(request.getParameter("birthday"));   //用来获取多个复选按钮的值   favorites = request.getParameterValues("favorites");   u.setUsername(username);   u.setMypassword(mypassword);   u.setGender(gender);   u.setEmail(email);   u.setIntroduce(introduce);   if (isAccept.equals("true")) {    u.setFlag(true);   }else {    u.setFlag(false);   }   u.setBirthday(birthday);   u.setFavorites(favorites);      //把注册成功的用户对象保存到session中   request.getSession().setAttribute("regUser", u);   //跳转到注册成功页面   request.getRequestDispatcher("../userinfo.jsp").forward(request,response);     } catch(Exception ex){   ex.printStackTrace();  }   } /**   * Initialization of the servlet.
   *   * @throws ServletException if an error occurs   */ public void init() throws ServletException {  // Put your code here }}package servlet;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import entity.Users;
public class RegServlet extends HttpServlet {
/**
   * Constructor of the object.
   */
public RegServlet() {
  super();
}
/**
   * Destruction of the servlet.

   */
public void destroy() {
  super.destroy(); // Just puts "destroy" string in log
  // Put your code here
}
/**
   * The doGet method of the servlet.

   *
   * This method is called when a form has its tag value method equals to get.
   *
   * @param request the request send by the client to the server
   * @param response the response send by the server to the client
   * @throws ServletException if an error occurred
   * @throws IOException if an error occurred
   */
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  doPost(request, response);
  
}
/**
   * The doPost method of the servlet.

   *
   * This method is called when a form has its tag value method equals to post.
   *
   * @param request the request send by the client to the server
   * @param response the response send by the server to the client
   * @throws ServletException if an error occurred
   * @throws IOException if an error occurred
   */
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  request.setCharacterEncoding("utf-8");
  
  Users u = new Users();
  String username;
  String mypassword;
  String gender;
  String email;
  String introduce;
  String isAccept;
  Date birthday;
  String[] favorites;
  
  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  
  try {
   username = request.getParameter("username");
   mypassword = request.getParameter("mypassword");
   gender = request.getParameter("gender");
   email = request.getParameter("email");
   introduce = request.getParameter("introduce");
   if (request.getParameterValues("isAccept")!=null) {
    isAccept = request.getParameter("isAccept");
   }else {
    isAccept = "false";
   }
   birthday = sdf.parse(request.getParameter("birthday"));
   //用来获取多个复选按钮的值
   favorites = request.getParameterValues("favorites");
   u.setUsername(username);
   u.setMypassword(mypassword);
   u.setGender(gender);
   u.setEmail(email);
   u.setIntroduce(introduce);
   if (isAccept.equals("true")) {
    u.setFlag(true);
   }else {
    u.setFlag(false);
   }
   u.setBirthday(birthday);
   u.setFavorites(favorites);
   
   //把注册成功的用户对象保存到session中
   request.getSession().setAttribute("regUser", u);
   //跳转到注册成功页面
   request.getRequestDispatcher("../userinfo.jsp").forward(request,response);
   
  } catch(Exception ex){
   ex.printStackTrace();
  }
  
}
/**
   * Initialization of the servlet.

   *
   * @throws ServletException if an error occurs
   */
public void init() throws ServletException {
  // Put your code here
}
}


这样怎么解决空指针?

免责声明:本内容仅代表回答者见解不代表本站观点,请谨慎对待。

版权声明:作者保留权利,不代表本站立场。

回复

使用道具 举报

参与会员1

这种问题,还是自己思考吧,多折腾几次,有好处
回复

使用道具 举报

可能感兴趣的问答

发新帖
  • 微信访问
  • 手机APP