Java 字符串到长转换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19977313/
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 String to Long Conversion
提问by DaRoGa
Simple problem i know but I cannot see what is wrong. I have the following code:
我知道一个简单的问题,但我看不出哪里出了问题。我有以下代码:
String dxorderId = request.getParameter("orderid");
long orderid = Long.parseLong((dxorderId));
It is inside a servlet which is called from a PL/SQL procedure. The value of dxorderid is retrieved from a parameter in the URL of the page called. Therefore it seems to be treated as a string. So I must convert the string into a long. However, when I use the code above, I get the following error:
它位于从 PL/SQL 过程调用的 servlet 中。dxorderid 的值是从被调用页面的 URL 中的参数中检索到的。因此它似乎被视为一个字符串。所以我必须将字符串转换为长字符串。但是,当我使用上面的代码时,出现以下错误:
14-Nov-2013 13:07:41 org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [com.zf.proceed.service.servlets.ViewOrderServlet] in context with path [/webapp] threw exception
java.lang.NumberFormatException: null
at java.lang.Long.parseLong(Long.java:372)
at java.lang.Long.parseLong(Long.java:461)
at com.zf.proceed.service.servlets.ViewOrderServlet.doRequest(ViewOrderServlet.java:28)
at com.zf.proceed.service.servlets.ProceedBaseServlet.doRequest(ProceedBaseServlet.java:106)
at com.zf.proceed.service.servlets.ProceedBaseServlet.doGet(ProceedBaseServlet.java:39)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:619)
I know it means that it cant be parsed but I dont Know why it cant. the value being input is 1454.
我知道这意味着它无法解析,但我不知道为什么它无法解析。输入的值为 1454。
采纳答案by christopher
I'm afraid the value is not 1454
. The value is null
, as evidenced by this line:
恐怕价值不是1454
。值是null
,正如这一行所证明的:
java.lang.NumberFormatException: null
You need to check your parameter name, and make sure you've got it right, including case and invisible characters.
您需要检查您的参数名称,并确保您输入正确,包括大小写和不可见字符。
回答by Ankur Shanbhag
Take a look at the exception closely : java.lang.NumberFormatException: null
仔细看看异常: java.lang.NumberFormatException: null
the above statement indicates that value for dxorderId
is null. Problem is with this line:
上面的语句表明值为dxorderId
空。问题出在这一行:
String dxorderId = request.getParameter("orderid");
回答by Aniket Kulkarni
java.lang.NumberFormatException: null
If String dxorderId
is null then it will throw NumberFormatException
如果 StringdxorderId
为 null 那么它会抛出 NumberFormatException
Solution
解决方案
Check dxorderId
is not null
and not empty string
检查dxorderId
是不是null
和不是empty string
if(dxorderId != null && !dxorderId.equals(""))
{
long orderid = Long.parseLong((dxorderId));
}
From oracle docs NumberFormatException
来自oracle 文档 NumberFormatException
public class NumberFormatException
extends IllegalArgumentException
Thrown to indicate that the application has attempted to convert a string to one of the numeric types, but that the string does not have the appropriate format.
抛出以指示应用程序已尝试将字符串转换为数字类型之一,但该字符串没有适当的格式。
回答by LionC
Well a NumberFormatException
means that the given Data can not be converted to a Long
because it does not represent a Long
-Value, so the problem lies at the source of the String, not in the Java Long-Parser.
嗯,aNumberFormatException
意味着给定的 Data 不能转换为 a,Long
因为它不代表一个Long
-Value,所以问题出在 String 的来源,而不是 Java Long-Parser。
In your specific case the Parser states that the given value was null
, so your request.getParameter("orderid");
return null
.
在您的特定情况下,解析器指出给定的值是null
,因此您request.getParameter("orderid");
返回null
.
回答by Mikhail
Use createLongor toLongmethod from Apache's NumberUtils.
使用createLong或toLong从Apache的NumberUtils方法。
回答by MouseLearnJava
java.lang.NumberFormatException: null
java.lang.NumberFormatException: null
From the exception information, we can learn that dxorderidis nullwhich causes java.lang.NumberFormatException
.
从异常信息中,我们可以了解到dxorderid为null导致java.lang.NumberFormatException
.
Before parsing data, you need to check whether the value to be parsed is nullor emptyfirst .
在解析数据之前,需要先检查要解析的值是 null还是 空。
回答by Kamlesh Arya
error is due to this line.
错误是由于这一行。
String dxorderId = request.getParameter("orderid");
Check whether parameter name "orderid" is correct or not or ensure that its value is not null
检查参数名称“orderid”是否正确或确保其值不为空