标准包装器值[register]:servlet的servlet.service()抛出异常java.lang.nullpointerexception;

时间:2015-03-31 14:51:23

标签: java servlets

我的程序返回一个空对象值,但我需要使用null对象值进行验证,但因为它是一个servlet页面,我尝试阻塞所以发生了什么,try尝试捕获异常并抛出零点异常。

         public class login extends HttpServlet {
                protected void processRequest(HttpServletRequest request,                          HttpServletResponse response)
       throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    String pass="",value="",email_id="";
    DBObject doc;
    try (PrintWriter out = response.getWriter()) {
        a=request.getParameter("email");
        out.println(a);
        b=request.getParameter("password");
        out.println(b);
        MongoClient client = new MongoClient();
        DB db = client.getDB("smart");
        DBCollection coll = db.getCollection("register");
      // here when am get a email id from the user in "a"  variable it matches in the database and retrieve it if the user not in db means it have to displayed user not in database bt it showing a null pointer expection bt i need to process the null 
        doc = coll.findOne(new BasicDBObject("email_id", a));
        //obj=doc.toString();

        email_id=doc.get("email_id").toString();

        pass = doc.get("Password").toString();

         key=doc.get("Key").toString();
       //here is the problem
         if((doc.toString()) == null)
        {
           out.println("user not in db");
           System.out.println("user not  in db");

        }
   }

}

2 个答案:

答案 0 :(得分:0)

你好,你有没有处理这个例外?如果是,为什么你没有包含那些代码?
如果没有请把catch块放在那里你必须检查该doc是否为null如果它是空打印"用户不在db&# 34。

答案 1 :(得分:0)

由于我发布了空指针异常,即使catch也没有捕获它,但是我做的错误是我已经验证它是否为null,在提交响应对象之后,所以当我检查doc对象时是否为null,它验证但我可以重定向到下一页是问题,响应已提交,因此在重定向到页面时也要检查