Search code examples
javaeclipsetomcatweb-applicationsnumberformatexception

HTTP Status 500 ? Internal Server Error Number Format Exception


Here is the servlet file, when i run it, numberformatexception is thrown, client is the class here.
output error when we put data in the fields, Don't know what to do. As I have tried all methods but it is still not working. As we post code here. you can review it.*

    Type Exception Report
    Message null
    Description The server encountered an unexpected condition that prevented it from fulfilling the request.
    Exception
        java.lang.NumberFormatException: null
        java.lang.Integer.parseInt(Unknown Source)
        java.lang.Integer.parseInt(Unknown Source)
        emlakcontroller.ClientServlet.doPost(ClientServlet.java:53)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
        org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    Note The full stack trace of the root cause is available in the server logs.
    
    

ClientServlet.java

    /**
    * Servlet implementation class ClientServlet
   */ 
    @WebServlet("/register")
    public class ClientServlet extends HttpServlet {
        private static final long serialVersionUID = 1L;
        private ClientDAO clientDao=new ClientDAO();
        /**
         * @see HttpServlet#HttpServlet()
         */
        public ClientServlet() 
        {
            super();
            // TODO Auto-generated constructor stub
        }
    
        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
        {
            // TODO Auto-generated method stub
            response.getWriter().append("Served at: ").append(request.getContextPath());
        }
        /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
        {   
            response.setContentType("text/html");  
            PrintWriter out = response.getWriter();     
            String name=request.getParameter("name");
            String email=request.getParameter("email");  
            String password=request.getParameter("password");  
            int contact=Integer.parseInt(request.getParameter("contact"));  
            Client client=new Client();
            client.setName(name);
            client.setEmail(email);
            client.setPassword(password);
            client.setContact(contact); 
            try 
            {
                clientDao.registerClient(client);
            } catch (ClassNotFoundException e)
              {
                // TODO Auto-generated catch block
                e.printStackTrace();
              }
              out.print("Yahoo! register successful");  
              RequestDispatcher rd=request.getRequestDispatcher("SignupFront.html");  
              rd.include(request,response); 
        }
    }
    

Solution

  • Most probably the request.getParameter("contact"); returns null and you try to parse it to integer. Try this:

    String contactStr = request.getParameter("contact");
    if (contactStr != null) {
        int contact = Integer.parseInt();  
    }