-1

Here is the code and i m getting the nullpointerexception in processrequest method and dopost method and i dont know how to resolve it. I am not getting any error in sending the email for verification code but when it checks whether the verification code is correct or not , it is throwing the null pointer exception.

package newpackage;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


public class VerifyCode extends HttpServlet {

   
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
           HttpSession session = request.getSession();
           User user = (User)session.getAttribute("authcode");
           String code = request.getParameter("authcode");
           if(code.equals(user.getCode())){
               response.sendRedirect("login.jsp");
           }
           else
           {
               out.println("Incorrect verification code");
           }
        }
    }

   
    @Override
   protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException{
        processRequest(request, response);
   }

 
}

Error:
 


SEVERE [http-nio-8080-exec-2] 
 org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() 
 for servlet [VerifyCode] in context with path [/WebApplication2] threw 
  exception
 java.lang.NullPointerException
at newpackage.VerifyCode.processRequest(VerifyCode.java:23)
at newpackage.VerifyCode.doPost(VerifyCode.java:37)
        
   
  • Here is the error:SEVERE [http-nio-8080-exec-70] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [VerifyCode] in context with path [/WebApplication2] threw exception java.lang.NullPointerException at newpackage.VerifyCode.processRequest(VerifyCode.java:23) at newpackage.VerifyCode.doPost(VerifyCode.java:37) – Arihant Jain May 05 '21 at 10:47
  • 1
    Make sure you [edit] your question to include the error. It should be in your main post, not as a comment. – Henry Twist May 05 '21 at 11:05
  • And point out which line in your code is line 23. Also see https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it – k314159 May 05 '21 at 11:34
  • Does your session really contains attribute `authcode` of type `User`? It can be null or `request.getParameter("authcode")` may return null. – Alex May 05 '21 at 12:34

0 Answers0