Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Servlet java.lang.NumberFormatException_Java_Servlets - Fatal编程技术网

Servlet java.lang.NumberFormatException

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

我在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(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=