Java Servlet NumberFormatException

问题描述 投票:0回答:1

我的代码中出现NumberFormatException,应该更新一个SQL列。当我尝试将其从字符串解析为int时,异常引用id为null。我不确定我在做什么错,因为如果有人可以帮助,在错误和代码出现之前,我使用的代码已在insert和remove子句中起作用。

Stacktrace:

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

        }
    }
java exception servlets number-formatting
1个回答
0
投票

看看这些方法是否对您有帮助:

  • 为NumberFormatException在catch块中执行某些操作
  • 也许添加一个执行parseInt的方法,并在解析失败的情况下返回默认值
  • 在调用parseInt()之前先进行空检查
© www.soinside.com 2019 - 2024. All rights reserved.