关于空指针错误的问题
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); Users u = new Users(); String username,mypassword,gender,email,introduce; boolean 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"); birthday = sdf.parse(request.getParameter("birthday")); isAccept=request.getParameter("isAccept")!=null?true:false; //用来获取多个复选按钮的值 favorites = request.getParameterValues("favorite"); u.setUsername(username); u.setMypassword(mypassword); u.setGender(gender); u.setEmail(email); u.setFavorites(favorites); u.setIntroduce(introduce); u.setFlag(isAccept); u.setBirthday(birthday); //把注册成功的用户对象保存在session中 request.getSession().setAttribute("regUser", u); //跳转到注册成功页面 request.getRequestDispatcher("../userinfo.jsp").forward(request,response); } catch(Exception ex) { ex.printStackTrace(); } }
一下两行代码 为什么使用第二行代码时不勾选复选框就会报空指针异常 而用第一行代码就能够正常返回false呢? 我的理解: 当不勾选时request.getParameter("isAccept").equals("true")该表达式的值为假那么则返回false
isAccept=request.getParameter("isAccept")!=null?true:false;
isAccept=request.getParameter("isAccept").equals("true")?true:false;