Java Servlet NumberFormatException
Posted
技术标签:
【中文标题】Java Servlet NumberFormatException【英文标题】: 【发布时间】:2020-05-06 09:19:25 【问题描述】:我的代码中出现 NumberFormatException,应该更新 SQL 列。当我尝试将其从字符串解析为 int 时,对 id 的异常引用为空。我不确定我做错了什么,因为我使用的代码在插入和删除子句之前起作用,如果有人可以提供帮助,这里是错误和代码。
堆栈跟踪:
INFO: Starting ProtocolHandler ["http-nio-8080"]
java.lang.NumberFormatException: null
at java.base/java.lang.Integer.parseInt(Integer.java:620)
at java.base/java.lang.Integer.parseInt(Integer.java:776)
at control.EditScoreServlet.doPost(EditScoreServlet.java:34)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:367)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1639)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.base/java.lang.Thread.run(Thread.java:830)
代码:
package control;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.Score;
import model.dao.ScoreDAO;
@WebServlet("/editscore")
public class EditScoreServlet extends HttpServlet
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
req.getRequestDispatcher("WEB-INF/editscore.jsp").forward(req, resp);
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
try
String name = req.getParameter("name");
String strDiff= req.getParameter("diff");
int diff= Integer.parseInt(strDiff);
String song = req.getParameter("song");
String grade = req.getParameter("grade");
String score = req.getParameter("score");
String groove = req.getParameter("groove");
int id = Integer.parseInt(req.getParameter("id"));
Score score = new Score(id, name, diff, song, grade, score, groove);
ScoreDAO scoredao = new ScoreDAO();
System.out.println(score.toString());
boolean updateSuccess= scoredao.editScore(tulos);
if (updateSuccess)
resp.sendRedirect("/listscores");
else
req.setAttribute("message", "An error occurred when updating a score");
req.getRequestDispatcher("WEB-INF/actionreport.jsp").forward(req, resp);
catch (NumberFormatException e)
// TODO Auto-generated catch block
e.printStackTrace();
catch (IOException e)
// TODO Auto-generated catch block
e.printStackTrace();
catch (ServletException e)
// TODO Auto-generated catch block
e.printStackTrace();
【问题讨论】:
id 的值为 null。请检查您是否在请求中传递了 id。 【参考方案1】:看看这些对你有没有帮助:
在 NumberFormatException 的 catch 块中做一些事情 可能会添加一个执行 parseInt 的方法,并在 parse-failures 的情况下返回默认值 在调用 parseInt() 之前进行空检查【讨论】:
以上是关于Java Servlet NumberFormatException的主要内容,如果未能解决你的问题,请参考以下文章
Java SE基础部分——常用类库之NumberFormat
使用 java.text.NumberFormat 解析日本货币字符串
Java 中非默认语言环境的 NumberFormat 解析不正确