开发者

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

开发者 https://www.devze.com 2023-04-06 02:45 出处:网络
I got exception: org.apache.jasper.JasperException: java.lang.NumberFormatException: null when trying this code:

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

<%@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 --%&g开发者_C百科t;
    <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.


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

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

精彩评论

暂无评论...
验证码 换一张
取 消