DaRoGa
DaRoGa

Reputation: 2404

Java String to Long Conversion

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:

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.

Upvotes: -1

Views: 24068

Answers (7)

Mengjun
Mengjun

Reputation: 3197

java.lang.NumberFormatException: null

From the exception information, we can learn that dxorderid is null which causes java.lang.NumberFormatException.

Before parsing data, you need to check whether the value to be parsed is null or empty first .

Upvotes: 0

Kamlesh Meghwal
Kamlesh Meghwal

Reputation: 4982

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

Upvotes: 0

Mikhail
Mikhail

Reputation: 4223

Use createLong or toLong method from Apache's NumberUtils.

Upvotes: 1

Aniket Kulkarni
Aniket Kulkarni

Reputation: 12993

java.lang.NumberFormatException: null  

If String dxorderId is null then it will throw NumberFormatException

Solution

Check dxorderId is not null and not empty string

if(dxorderId != null && !dxorderId.equals(""))
{
  long orderid = Long.parseLong((dxorderId));
}  

From oracle docs 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.

Upvotes: 3

LionC
LionC

Reputation: 3106

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.

In your specific case the Parser states that the given value was null, so your request.getParameter("orderid"); return null.

Upvotes: 0

Ankur Shanbhag
Ankur Shanbhag

Reputation: 7804

Take a look at the exception closely : java.lang.NumberFormatException: null

the above statement indicates that value for dxorderId is null. Problem is with this line:

String dxorderId = request.getParameter("orderid");

Upvotes: 0

christopher
christopher

Reputation: 27356

I'm afraid the value is not 1454. The value is null, as evidenced by this line:

java.lang.NumberFormatException: null

You need to check your parameter name, and make sure you've got it right, including case and invisible characters.

Upvotes: 3

Related Questions