您的位置:首页 > 编程语言 > Java开发

java异常java.lang.reflect.InvocationTargetException 并且同时带有空指针异常

2017-10-20 23:48 225 查看
java.lang.reflect.InvocationTargetException 并且同时带有空指针异常,

该错误来源于,实现用户自动登录功能时:登录界面,当输入用户名和密码后,没有勾选自动登录按钮,结果在UserServlet中实现方法时,只要有getParameter(“autologin”);就报错,

分析:用过滤器实现对getParameter方法包装设计增强时,没有加入空指针异常判断。所以在servlet方法中使用自定义的getParameter时,当用户没有勾选自动登录按钮,获取到的是空。所以会报此错误。

解决:在对getparameter方法包装设计进行功能增强时,加入对获取的参数进行空指针判断。或者把异常抛掉一个大异常catch(Exception e){}

class MyRequest extends HttpServletRequestWrapper {

public MyRequest(HttpServletRequest request) {

super(request);

}

@Override

public String getParameter(String name) {

String word = super.getParameter(name);

try {

if (word == null) {

return null;

}

word = new String(word.getBytes("ISO-8859-1"), "UTF-8");

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

} catch (Exception e) {

e.printStackTrace();

}

return word;

}}

异常代码截图:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐