java - Servlet java.lang.NumberFormatException

标签 java servlets

我在 servlet 上收到两个参数,我需要将它们作为字符串放入 PreparedStatement 中我可以使用setInt(1, parameter) .

public class rate extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        response.setContentType("text/html;charset=UTF-8");
        PreparedStatement pstmt = null;
        Connection con = null;
       //FileItem f1;
        String id = request.getParameter("idbook");
        String opcoes = request.getParameter("voto");
        int idlivro=Integer.parseInt(id);
        int opcao = Integer.parseInt(opcoes);
        String updateString = "Update rating set livros_rate = ? where productId = ?";
        if (idlivro != 0)
     {

          try {
         //connect to DB 
         con = login.ConnectionManager.getConnection();
         pstmt = con.prepareStatement(updateString);
         pstmt.setInt(1, opcao);
         pstmt.setInt(2, idlivro);
         pstmt.executeUpdate();
      }
          catch (Exception e){
      }finally{
             try {
                 pstmt.close();
                 con.close();
             } catch (SQLException ex) {
                 Logger.getLogger(rate.class.getName()).log(Level.SEVERE, null, ex);
             }

          }
     }
}

但是,它会引发以下异常:

java.lang.NumberFormatException: null
    at java.lang.Integer.parseInt(Integer.java:454)
    at java.lang.Integer.parseInt(Integer.java:527)
    at counter.rate.doPost(rate.java:45)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728

这是怎么造成的,如何解决?

编辑:我将放置发送参数的表单代码。

<div id="templatemo_content_right">
    <div class="templatemo_product_box">
            <h1><%=rs.getString(3)%>  <span>(<%=rs.getString(7)%>)</span></h1>
        <img src="<%=rs.getString(13)%>"/>
            <div class="product_info">
                <p><%=rs.getString(10)%></p>
                   <div><form action="rate"  method="POST">
                            <imput type="hidden" name="idbook" value="<%=rs.getString(1)%>"/>
                            <select name="voto">
                                <option value="0">Did not like</option>
                                <option value="1">Ok</option>
                                <option value="2" selected="selected">Liked</option>
                                <option value="3">Loved!</option>
                            </select>
                            <input type="submit" value="Votar!"/>
                    </form></div>

是否是表单导致了问题?

最佳答案

异常是非常具有描述性的,您正在尝试将 null 解析为 int。在调用 parseInt()

之前进行空检查
String id = request.getParameter("idbook");
    String opcoes = request.getParameter("voto");
int idlivro=0;    
if(id!=null)
   idlivro =Integer.parseInt(id);
    int opcao =0;
if(opcoes!=null)
 opcao=Integer.parseInt(opcoes);

OneLiner:

int idlivro  = (id!=null) ? Integer.parseInt(id) : 0;

关于java - Servlet java.lang.NumberFormatException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14961662/

相关文章:

java - jsp页面只显示字符串中的一个单词

JavaBean 到 CSV 文件

java.util.timer定时器空指针异常

java - RGBIR 的 Tango 相机预览

java - Apache Velocity - 由于 slf4j 依赖性而无法评估脚本

java - 从 Java Servlet 向 Web API 发送 get 请求

jakarta-ee - WebSphere Application Server 8.5 上的 SRVE0200E 错误

java - 尝试为我的代码获取正确的值答案

java - 如何在所有相等元素之后快速将元素插入到具有重复项的数组中?

java - 以编程方式设置 web.xml 配置