0

サーブレットで2つのパラメーターを受け取ります。これらのパラメーターは、で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>

フォームが問題を引き起こしていますか?

4

5 に答える 5

4

例外はかなり説明的です。null を int として解析しようとしています。呼び出す前に null チェックを行う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);

一発ギャグ:

int idlivro  = (id!=null) ? Integer.parseInt(id) : 0;
于 2013-02-19T15:57:37.987 に答える
0
int idlivro=Integer.parseInt(id);
int opcao = Integer.parseInt(opcoes);

idまたはopcaoがnullであるか、スペースが含まれている可能性があります。したがって、java.lang.NumberFormatExceptionが発生します。

考えられるケース:

id ="";
id = "  123";
id=null;
于 2013-02-19T15:59:13.907 に答える
0

期待しているパラメーターを取得していないと思われます (request.getParameter("idbook"); が null のように)。

簡単なテスト クラスを次に示します。

public class IntProb {

public static final String SOME_STRING = "888";
public static final String NULL_STRING = null;

public static void main(String[] argv) {

    System.out.println("-------- Testing parseInt ------------");

    System.out.println("converting SOME_STRING: ");
    try{
        int intSomeInt = Integer.parseInt(SOME_STRING);

    } catch(Exception e){
        e.printStackTrace();
    }

    System.out.println("converting NULL_STRING: ");
    try{
        int intSomeInt = Integer.parseInt(NULL_STRING);

    } catch(Exception e){
        e.printStackTrace();
    }

    System.out.println("-------- End of parseInt Test ------------");

}

}

$ javac IntProb.java $ java IntProb

収量:

-------- Testing parseInt ------------
converting SOME_STRING: 
converting NULL_STRING: 
java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Integer.java:417)
at java.lang.Integer.parseInt(Integer.java:499)
at IntProb.main(IntProb.java:20)
-------- End of parseInt Test ------------

null を parseInt に渡そうとするとうまくいきません。

于 2013-02-19T16:03:56.543 に答える
0

NumberFormatExceptionアプリケーションが a を数値型に変換しようとしたStringが、解析された の形式が正しくないために失敗したことを示すために、 aがスローされStringます。

あなたのコードでは、問題のある可能性のある行は次のとおりです。

int idlivro=Integer.parseInt(id);
int opcao = Integer.parseInt(opcoes);

そのうちの 2 つはパラメーターであり、リクエストから直接取得されます。

String id = request.getParameter("idbook");
String opcoes = request.getParameter("voto");

これらのパラメーターの形式を確認してください (特に、それらが または でないかどうかを確認してくださいnull) 。または、それらを制御できない場合は、ブロック"null"でそれをキャッチしてください。NumberFormatExceptiontry-catch

//Code
try {
    //More Code
    int idlivro=Integer.parseInt(id);
    int opcao = Integer.parseInt(opcoes);
    //More Code
} catch(NumberFormatException nfe) {
    //Logic that should be executed if the book or the option are invalid.
}

もちろん、必要に応じて、個々の解析を試してキャッチすることもできます。別の方法として (そして、パラメーターを取得していないことがわかっている場合null)、次のように、正規表現を使用して、文字列を解析する前に検証することができます (よりクリーンなアプローチ、IMHO)。

String id = request.getParameter("idbook");
String opcoes = request.getParameter("voto");
//...
if(Pattern.matches("/^\d+$/", id)) {
    //Safe parse here.
}    

必要な方法でコードをビルドしますが、解析の前にこのチェックを追加することで、NUmberFormatException.

于 2013-02-19T16:06:14.437 に答える