In the servlet, in place of " MusicType musicType = musicTypeDAO.read(Integer.parseInt(request.getParameter("musicTypes")));" NumberFormatException, but I don't know why View:

  <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.util.*"%> <%@page import="Model.MusicType"%> <%@ page import="DAO.MusicTypeDAO"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"> </head> <body> <% MusicTypeDAO dao = new MusicTypeDAO(); List<MusicType> musicTypes = dao.getAll(); %> <h1 align="center">Registration</h1> <p align="center">${errorsRegistr}</p> <table align="center"> <form action="RegistrServlet" name="RegistrServlet" method="GET"> <tr> <td>Login:</td> <td><input type="text" name="login" size="10"></td> </tr> <tr> <td>Password:</td> <td><input type="text" name="password" size="10"></td> </tr> <tr> <td>Confirm password:</td> <td><input type="text" name="confirmPassword" size="10"></td> </tr> <tr> <td>First name:</td> <td><input type="text" name="firstName" size="10"></td> </tr> <tr> <td>Last name:</td> <td><input type="text" name="lastName" size="10"></td> </tr> <tr> <td>Age:</td> <td><input type="text" name="age" size="10"></td> </tr> <tr> <td>Counry:</td> <td><input type="text" name="country" size="10"></td> </tr> <tr> <td>Street:</td> <td><input type="text" name="street" size="10"></td> </tr> <tr> <td>Zip code:</td> <td><input type="text" name="login" size="10"></td> </tr> <tr> <td>Music type:</td> <td><select name="musicTypes"> <% for (MusicType music : musicTypes) { %> <option value="<%=music.getId()%>"><%=music.getTypeName()%></option> <% } %> </select></td> </tr> <tr> <td><input type="submit" value="Создать"></input> </form></td> <td><a href="index.jsp"><input type="submit" value="Вернуться на главную"></a></td> </tr> </table> </body> </html> 

Servlet

 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { RoleDAO roleDAO = new RoleDAO(); MusicTypeDAO musicTypeDAO = new MusicTypeDAO(); UserDAO userDAO = new UserDAO(); String login = request.getParameter("login"); String password = request.getParameter("password"); String firstName = request.getParameter("firstName"); String lastName = request.getParameter("lastName"); String ageStr = request.getParameter("age"); String country = request.getParameter("country"); String street = request.getParameter("street"); String zipStr = request.getParameter("zip"); MusicType musicType = musicTypeDAO.read(Integer.parseInt(request.getParameter("musicTypes")));//вот тут нул поинтер Address address = new Address(1, country, street, Integer.parseInt(zipStr)); Role role = new Role(3, "User"); HashSet<MusicType> musicTypes = new HashSet<MusicType>(); musicTypes.add(musicType); User user = new User(1, login, password, firstName, lastName, Integer.parseInt(ageStr), address, role, musicTypes); userDAO.create(user); HttpSession session = request.getSession(true); session.setAttribute("id", user.getId()); request.getRequestDispatcher("/UserServlet").forward(request, response); } 

Error stack:

 SEVERE: Servlet.service() for servlet [RegistrServlet] in context with path [/MusicStore] threw exception java.lang.NumberFormatException: null at java.lang.Integer.parseInt(Integer.java:542) at java.lang.Integer.parseInt(Integer.java:615) at Servlets.RegistrServlet.doGet(RegistrServlet.java:45) at javax.servlet.http.HttpServlet.service(HttpServlet.java:622) at javax.servlet.http.HttpServlet.service(HttpServlet.java:729) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:230) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:108) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:522) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:349) at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:1110) at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:785) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1425) at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:745) фев 21, 2017 8:38:05 PM org.apache.catalina.core.StandardContext reload INFO: Reloading Context with name [/MusicStore] has started фев 21, 2017 8:38:05 PM org.apache.catalina.loader.WebappClassLoaderBase clearReferencesJdbc WARNING: The web application [MusicStore] registered the JDBC driver [com.mysql.jdbc.Driver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered. фев 21, 2017 8:38:05 PM org.apache.catalina.loader.WebappClassLoaderBase clearReferencesThreads WARNING: The web application [MusicStore] appears to have started a thread named [MySQL Statement Cancellation Timer] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread: java.lang.Object.wait(Native Method) java.lang.Object.wait(Object.java:502) java.util.TimerThread.mainLoop(Timer.java:526) java.util.TimerThread.run(Timer.java:505) фев 21, 2017 8:38:08 PM org.apache.jasper.servlet.TldScanner scanJars INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time. фев 21, 2017 8:38:08 PM org.apache.catalina.core.StandardContext reload INFO: Reloading Context with name [/MusicStore] is completed 
  • try using String.valueOf (your value) there, it usually appears when you try to parse a string as a number - java
  • here's stackoverflow.com/questions/18711896 look again - java
  • I see the NumberFormatException as if and not NullPointerException ... - Bohdan Korinnyi

1 answer 1

The stacktrace error is not a NullPointerException but a NumberFormatException .

Occurs because request.getParameter("musicTypes") returns null , not a string.

  • I understood that, but why is this happening? - Vitaly Litvinov
  • because you do not transfer this field with jsp? or is it out of shape? - Bohdan Korinnyi
  • as I see all the other fields are input s, and musicTypes is not, then the problem is here - Bohdan Korinnyi