servlet的异常问题

来源:百度知道 编辑:UC知道 时间:2024/05/05 02:35:58
这是我的doGet方法
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String aim = request.getAttribute("aim").toString();
if(aim.equals("aa")){
response.sendRedirect("../aa.jsp");
}
else if(aim.equals("bb")){
response.sendRedirect("../bb/cc.jsp");
}
}

报错页面为
HTTP Status 500 -

--------------------------------------------------------------------------------

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

java.lang.NullPointerException
aa.check.doGet(check.java:25)
javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

note The full stack tra

String aim = request.getAttribute("aim").toString();
很明显没获得页面提交的数据,aim为空
可改为
if("aa".equals(aim))
程序中这么写可避免很多空指针异常

是不是这行报的异常?
if(aim.equals("aa")){
那是在请求中没有aim这个参数
检查一下参数就可以了

radio的话,应该是
request.getParameter("aim").

你的aim是参数还是属性?别搞混了!
String aim = request.getAttribute("aim").toString();
上面aim为空,下面就报错了,你可以先测试下aim是否为空
if(aim.equals("aa")){

楼主,估计你也是java新手
我觉得作为初学者不要一遇到问题就寄托于别人
首先自己一定要有个思考的过程
报空指针异常,就这么几行代码不妨多System.out.println()一把,
check.java:25,就在25行输出咯
相信你会自己解决了的

null pointer exception---空指针异常,是因为程序在比较时并没有从request范围中取到相应的值,所以在你用equals方法比较时,请将字符串放在前面,用字符串去比较aim变量,例如:"aa".equals(aim),这样可以避免空指针异常,