org.apache.jasper.JasperException: java.lang.NumberFormatException: null

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7474322/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 20:08:40  来源:igfitidea点击:

org.apache.jasper.JasperException: java.lang.NumberFormatException: null

javaexceptionnullnumbers

提问by inggar

I got exception: org.apache.jasper.JasperException: java.lang.NumberFormatException: nullwhen trying this code:

尝试此代码时出现异常:org.apache.jasper.JasperException:java.lang.NumberFormatException:null

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
</head>
<body>

    <h3>Tanggal sekarang:</h3>
    <%= new java.util.Date() %>
    <% //begin scriplet
    String nama = request.getParameter( "namadepan" );
    String id = request.getParameter("idUser");
    int idInt = Integer.parseInt(id);
    if(nama!=null) {

    %> <%-- end scriplet to insert fixed template data --%>
    <h3>Halo <%= nama %>, <br/>
        int idInt = Integer.parseInt(idUser);
        Selamat datang di web Koperasi Aneka Usaha!
        id anda :<%=idInt %>
        </h3>
    <% //continue scriptlet
    }//end if
    else {
    %> <%-- end scriplet to insert fixed template data --%>
<form name="login" action="index.jsp" method="post">
<table width="200" border="1">
<tr>
<td width="56">Nama </td>
<td width="128"><input type="text" name="namadepan"></td>
</tr>
<tr>
<td>ID</td>
<td><input type="text" name="idUser"></td>
</tr>
<tr>
<td>Umur</td>
<td><input type=int name="umur"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</form>
    <% //continue scriptlet
    }//end else
    %> <%-- end scriplet --%>

</body>
</html>

How to fix that exception? im sure the error is about convertint input type string (idUser) to integer.

如何修复该异常?我确定错误是关于将输入类型字符串(idUser)转换为整数的。

回答by nfechner

The problem is the call to Integer.parseInt(id);. If idis not a parsable number, a NumberFormatExceptionwill be thrown. In your case because idwas null. You should at least include a check for null, but it would be better do to something like:

问题是调用Integer.parseInt(id);. 如果id不是可解析的数字,NumberFormatException则将抛出 a。在你的情况下,因为idnull。您至少应该包括对 null 的检查,但最好执行以下操作:

int idInt = 0; // Or a different default value
try {
    idInt = Integer.parseInt(id);
} catch(NumberFormatException e) {
    // log the error or ignore it
}