java.lang.NumberFormatException: null
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15679411/
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
java.lang.NumberFormatException: null
提问by Bhavya Londhe
I have created a login form which contains validations for each field. When i click on submit button function validation() will be invoked to validate all the fields and after successful validation it will redirect to another jsp page where all the details will be inserted in to the Oracle database.
我创建了一个登录表单,其中包含每个字段的验证。当我单击提交按钮时,将调用函数验证()以验证所有字段,验证成功后它将重定向到另一个 jsp 页面,其中所有详细信息都将插入到 Oracle 数据库中。
But I'm getting "org.apache.jasper.JasperException: java.lang.NumberFormatException: null" exception. Also "The server encountered an internal error () that prevented it from fulfilling this request" error. I hope you will help me.
但我收到“org.apache.jasper.JasperException:java.lang.NumberFormatException:null”异常。还有“服务器遇到内部错误 () 阻止它完成此请求”错误。我希望你能帮助我。
Here is the code:
这是代码:
<html>
<head>
<script type="text/javascript">
function validate()
{
if(document.frm.username.value=="")
{
alert("Please enter Username");
document.frm.username.focus();
}
else if(document.frm.mobile.value=="")
{
alert("Please Enter your contact number");
document.frm.mobile.focus();
}
else
{
window.location = "insert.jsp";
}
}
</script>
</head>
<body>
<form name="frm">
<table>
<tr><td>User Name:</td><td><input type="text" name="username"></td></tr>
<tr><td>Contact Number:</td><td><input type="text" name="mobile"></td></tr>
<tr><td><input type="submit" value="Submit" onclick="validate()"></td><td></td></tr>
</table>
</form>
</body>
insert.jsp:
插入.jsp:
<body>
<%@page import="java.sql.*"%>
<%@page import="java.util.*"%>
<%
Connection con=null;
int mobile=Integer.parseInt(request.getParameter("mobile"));
String username=request.getParameter("username");
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","manager");
Statement st=con.createStatement();
st.executeUpdate("insert into stud values("+mobile+",'"+username+"')");
out.println("Data is successfully inserted!");
}
catch(Exception e)
{
System.out.print(e);
}
%>
</body>
回答by Brian Agnew
So what will happen if your mobile number is blank ?
那么,如果您的手机号码是空白的,会发生什么?
int mobile=Integer.parseInt(request.getParameter("mobile"));
You're asking Integer.parseInt()
to parse an empty or null string and that's causing your problem.
您要求Integer.parseInt()
解析空字符串或空字符串,这导致了您的问题。
From the doc:
从文档:
Throws: NumberFormatException - if the string does not contain a parsable integer.
抛出: NumberFormatException - 如果字符串不包含可解析的整数。
You need to check that mobile
is populated and.or handle the scenario when it's not.
您需要检查是否mobile
已填充并且.或在未填充时处理该场景。
I wouldn't use an Integer
to store a mobile number, btw. The number of digits could cause an overflow and/or you may want to maintain structure (e.g. country code and the number) etc.
Integer
顺便说一句,我不会使用 an来存储手机号码。位数可能会导致溢出和/或您可能想要维护结构(例如国家/地区代码和号码)等。
回答by mthmulders
You're redirecting the browser to do a GET
on insert.jsp
, but you're not supplying the request parameters to that new URL. Thus, your JSP fetches the mobile
request parameter, which is null
, and then tries to parse that to an integer, yielding the NumberFormatException
.
您正在重定向浏览器以执行GET
on insert.jsp
,但您没有向该新 URL 提供请求参数。因此,您的 JSP 获取mobile
请求参数,即null
,然后尝试将其解析为整数,生成NumberFormatException
.
What you could do is append the request parameters to the URL, like so:
您可以做的是将请求参数附加到 URL,如下所示:
window.location = "insert.jsp?mobile=" + document.frm.mobile.value + "&username=" + document.frm.username.value;
But it would be even better to submit those values in a POST
request, instead of a GET
. I think you could achieve that by adding a action="insert.jsp"
attribute to the form
tag, changing the onClick
attribute to onSubmit
and removing the
但最好在POST
请求中提交这些值,而不是GET
. 我认为您可以通过向标签添加action="insert.jsp"
属性form
,将onClick
属性更改为onSubmit
并删除
else {
window.location = "insert.jsp";
}
because that would allow the browser to resume its normal form submission. If you combine that with an return false;
statement after focussing on the empty fields, you'll prevent the browser from submitting the form.
因为这将允许浏览器恢复其正常的表单提交。如果您return false;
在关注空字段后将其与语句结合使用,您将阻止浏览器提交表单。
回答by Tu Tran
First, your code is very dangerous. You should check null or empty on the server side! Using PreparedStatement instead of Statement.
首先,您的代码非常危险。您应该在服务器端检查 null 或 empty !使用 PreparedStatement 而不是 Statement。
Second, the code window.location = "insert.jsp";
will not work as your expectation.
Use action="insert.jsp" to make data send to that page. Then, on your js function, return false if it does not pass the condition, otherwise, return true;
其次,代码window.location = "insert.jsp";
不会像您期望的那样工作。使用 action="insert.jsp" 将数据发送到该页面。然后,在你的js函数上,如果不通过条件则返回false,否则返回true;
Using onSubmit="return validate()" instead of onClick event.
使用 onSubmit="return validate()" 而不是 onClick 事件。
回答by kamituel
If you are using parseInt()
, you should catch an exception somewhere:
如果您正在使用parseInt()
,您应该在某处捕获异常:
int mobile;
try {
mobile = Integer.parseInt(request.getParameter("mobile"));
} catch (NumberFormatException e) {
// do something
}
In your case, request.getParameter("mobile")
is probably returning null.
在您的情况下,request.getParameter("mobile")
可能返回 null。
Edit:as nother already noted - storing phone number in an integer may not be a good idea. Try Long
instead.
编辑:正如其他人已经指出的那样 - 将电话号码存储在整数中可能不是一个好主意。试试吧Long
。