简体   繁体   中英

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

my program returns a null object value but i need to use the null object value for verification but because it is a servlet page and i have try block so whats happening the try catches the exception and throwing null point exception.

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

        }
   }

}

Hi
Did you handled the exception ? if yes why you didn't include that code?
if No please put catch block there you have to check whether that doc is null or not if it is null print "user is not in db".

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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