Servlet java.lang.NumberFormatException
我在servlet上接收到两个参数,需要将它们作为字符串,以便在Servlet java.lang.NumberFormatException,java,servlets,Java,Servlets,我在servlet上接收到两个参数,需要将它们作为字符串,以便在PreparedStatement中使用setInt(1,参数) 但是,它会引发以下异常: java.lang.NumberFormatException: null at java.lang.Integer.parseInt(Integer.java:454) at java.lang.Integer.parseInt(Integer.java:527) at counter.rate.doPost(rat
PreparedStatement
中使用setInt(1,参数)
但是,它会引发以下异常:
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()
OneLiner:
int idlivro = (id!=null) ? Integer.parseInt(id) : 0;
异常非常具有描述性,您正试图将null解析为int。在调用
parseInt()之前,请执行null检查。
OneLiner:
int idlivro = (id!=null) ? Integer.parseInt(id) : 0;
可能是id,或者opcao为null,或者其中有空格。因此您得到了java.lang.NumberFormatException
可能的情况:
id ="";
id = " 123";
id=null;
可能是id,或者opcao为null,或者其中有空格。因此您得到了java.lang.NumberFormatException
可能的情况:
id ="";
id = " 123";
id=null;
我怀疑您没有获得期望的参数(如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
yeilds:
-------- 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的过程很糟糕。我怀疑您没有获得期望的参数(如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
yeilds:
-------- 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会出错。抛出一个
NumberFormatException
,表示应用程序试图将字符串
转换为数字类型,但由于解析的字符串的格式不正确而失败
在您的代码中,可能出现的违规行如下:
int idlivro=Integer.parseInt(id);
int opcao = Integer.parseInt(opcoes);
其中两个是直接从请求中获得的参数:
String id = request.getParameter("idbook");
String opcoes = request.getParameter("voto");
请检查这些参数的格式(特别是,检查它们是否不是null
或“null”
),或者,如果您无法控制它们,请使用try catch
块捕获该NumberFormatException
:
//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
引发NUmberFormatException
以指示应用程序试图将字符串
转换为数字类型,但由于解析的格式不正确而失败字符串
在您的代码中,可能出现的违规行如下:
int idlivro=Integer.parseInt(id);
int opcao = Integer.parseInt(opcoes);
其中两个是直接从请求中获得的参数:
String id = request.getParameter("idbook");
String opcoes = request.getParameter("voto");
请检查这些参数的格式(特别是,检查它们是否不是null
或“null”
),或者,如果您无法控制它们,请使用try catch
块捕获该NumberFormatException
:
//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.
}
按照需要的方式构建代码,但通过在解析之前添加此检查,您可以避免处理表单第8行上的NUmberFormatException
语法错误,可能会导致getRequest出现问题
见:
输入!=输入表单第8行出现语法错误,可能导致getRequest出现问题
见:
输入!=输入最有可能发生的情况是您没有名为idbook
的请求参数。当您尝试解析它时,它为null,因此抛出NumberFormatException
。请使用调试器检查任何参数是否为null
。最可能发生的情况是什么最重要的是你没有一个名为idbook
的请求参数。当你试图解析它时,它是空的,因此抛出numberformatception
。请使用调试器检查是否有任何参数是null
。我经常在try{…}catch(numberformatception e)中包装Integer.parseInt();
{…}
因为像“moof”这样的坏字符串"将导致NumberFormatException
。通常,如果解析失败,我会有一个默认值或某种行为想要触发。@MichaelShopsin NFE是一个未经检查的运行时异常。您真的不需要将其包装为try/catch.:)真正的问题不是处理null
值参数,而是指导操作为什么不将参数发送到服务器端。此外,如果其中一个参数确实为null
,则OP应返回错误消息,而不是使用0
值(可能不返回任何值)@LuiggiMendoza回答说,我们需要查看OP在请求中设置值的代码:)@PremGenError通常抑制错误是不好的,但在这种情况下,NFE确实应该是一个错误返回,以便您决定是否为异常。例如,如果您希望count默认为0,但允许参数覆盖它:int count=