gpt4 book ai didi

java - 为什么我收到 NumberFormatException

转载 作者:行者123 更新时间:2023-11-29 13:10:48 25 4
gpt4 key购买 nike

您好,我只是想知道为什么当我尝试运行这个 servlet 时会收到 NumberFormatException...我已经尝试了很多不同的事情,现在正在寻求一些帮助。谢谢。

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

}

这是 Tomcat 日志:

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.

最佳答案

您正在尝试将空字符串解析为无效的数字

java.lang.NumberFormatException: For input string: ""

您可能想在请求数据上添加服务器端 validator

关于java - 为什么我收到 NumberFormatException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22125216/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com