繁体   English   中英

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

[英]standard wrapper value[register]:servlet.service() for servlet threw exception java.lang.nullpointerexception;

我的程序返回一个空对象值,但是我需要使用该空对象值进行验证,但是因为它是一个servlet页面,并且我有try块,所以try发生的事情捕获了异常并抛出了null点异常。

         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");

        }
   }

}

你好
您是否处理了例外? 如果是,为什么不包括该代码?
如果否,请在其中放置catch块,如果该文档为null,则必须检查该文档是否为null打印“用户不在数据库中”。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM