Reputation: 99
Hello I just want to know why am I getting a NumberFormatException when I try to run this servlet... I tried a lot of different things already and now is looking for some help. Thanks.
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String artistName = request.getParameter("artistName");
String songTitle = request.getParameter("songTitle");
String albumTitle = request.getParameter("albumTitle");
String yearReleased = request.getParameter("yearReleased");
String weekAtNumberOne = request.getParameter("weekAtNumberOne");
int yearReleasedInt = Integer.parseInt(yearReleased);
int weekAtNumberOneInt = Integer.parseInt(weekAtNumberOne);
ServletContext sc = this.getServletContext();
String dbDriver = sc.getInitParameter("driver");
String dbURL = sc.getInitParameter("url");
String dbName = sc.getInitParameter("database");
String dbUser = sc.getInitParameter("dbusername");
String dbPassword = sc.getInitParameter("dbpassword");
DBConnector.createConnection(dbDriver, dbURL, dbName, dbUser, dbPassword);
Song song = new Song(artistName, songTitle, weekAtNumberOneInt , yearReleasedInt, albumTitle);
SongDAO songDAO = new SongDAO();
if (songDAO.ArtistCheck(song) == true)
{
RequestDispatcher rd = request.getRequestDispatcher("/addNewArtist.jsp");
rd.forward(request, response);
}
else
{
songDAO.AddNewSong(song);
RequestDispatcher rd = request.getRequestDispatcher("GetAllSongs.do");
rd.forward(request, response);
}
}
here's the Tomcat Log:
Mar 02, 2014 12:28:51 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [AddNewSingleServlet] in context with path [/test] threw exception
java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:504)
at java.lang.Integer.parseInt(Integer.java:527)
at com.assignment2.Servlets.AddNewSingleServlet.doPost(AddNewSingleServlet.java:67)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:409)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1044)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:315)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744)
String dbName = sc.getInitParameter("database"); is line 67 in the error.
Upvotes: 0
Views: 2913
Reputation: 8657
You're getting this exception because you're trying to parse an empty string, i suspect in this parameters:
String yearReleased = request.getParameter("yearReleased");
String weekAtNumberOne = request.getParameter("weekAtNumberOne");
Make sure that the request params are not empty then try to parse them
int yearReleasedInt = Integer.parseInt(yearReleased);
if(yearReleasedInt != null || !yearReleasedInt.equals("")) {
int yearReleasedInt = Integer.parseInt(yearReleased);
}
Or you could catching the exception without doing a validation on the params, but that depends on you business case.
int yearReleasedInt = 0;
try {
yearReleasedInt = Integer.parseInt(yearReleased);
} catch(NumberFormatException ex) {
//handle exception
}
Upvotes: 0
Reputation: 29806
Either of:
int yearReleasedInt = Integer.parseInt(yearReleased);
int weekAtNumberOneInt = Integer.parseInt(weekAtNumberOne);
or both maybe causing the error. Try to print yearReleased
and weekAtNumberOne
and see the String
value.
You can do something as:
int yearReleasedInt = -1;
try {
yearReleasedInt = Integer.parseInt(yearReleased);
} catch(Exception ex){
//handle error
}
if(yearReleasedInt != -1) {
// do your work.
}
Upvotes: 0
Reputation: 240898
you are attempting to parse empty string to Number which isn't valid
java.lang.NumberFormatException: For input string: ""
You might want to add server side validator on your request data
Upvotes: 1