HTTP 状态 500 ?内部服务器错误编号格式异常

HTTP Status 500 ? Internal Server Error Number Format Exception

这里是servlet文件,当我运行它的时候,numberformaexception被抛出,client是这里的class。
当我们将数据放入字段时输出错误,不知道该怎么办。因为我已经尝试了所有方法,但它仍然无法正常工作。正如我们 post 在这里编写代码一样。您可以查看它。*

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

返回的堆栈跟踪应该足以解决此问题。

如您所见,需要注意以下几点:

Exception
        java.lang.NumberFormatException: null

这有效地告诉您,由于值为 null,因此转换为数字失败。从您提供的代码中,唯一可能的地方如下:

int contact=Integer.parseInt(request.getParameter("contact")); 

因此您应该执行以下操作:

  1. 确保 request.getParameter("contact") returns 一个 非空 值。
  2. 确保 request.getParameter("contact") returns 一个可以解析为 Integer 的值,否则该过程将失败并出现相同的错误。

很可能是 request.getParameter("contact"); returns null 并且您尝试将其解析为整数。试试这个:

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