This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes How to solve error regarding string to double transforming? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to solve error regarding string to double transforming?" Watch "How to solve error regarding string to double transforming?" New topic
Author

How to solve error regarding string to double transforming?

Ziya Bakan
Greenhorn

Joined: Feb 07, 2013
Posts: 13
Hi all!

This part of my code gives an error like:

java.lang.NullPointerException
at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
at java.lang.Double.parseDouble(Unknown Source)

and this is caused by this line of my code :

String mystring= request.getParameter("mystring");
Double mystring = Double.parseDouble(mystring); // here is the ERROR

I will be grateful if you could help, because i gotta solve this problem urgently.
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2229
    
    7

The error you got is self-explanatory. Is the variable "mystring" null BEFORE passing into the Double.parseDouble() method? You will also get problem if mystring variable is a empty string "" when passing into parse method.

K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5
Ziya Bakan
Greenhorn

Joined: Feb 07, 2013
Posts: 13
K. Tsang wrote:The error you got is self-explanatory. Is the variable "mystring" null BEFORE passing into the Double.parseDouble() method? You will also get problem if mystring variable is a empty string "" when passing into parse method.


No, mystring includes a number which has type string.so it s not null.
Ziya Bakan
Greenhorn

Joined: Feb 07, 2013
Posts: 13
I assigned a value into the quotes like "0.5" to mystring, and it works...It seems like that the value of mystring isnt passed to parseDouble()..Any ideas why ?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37884
    
  22
It is a bad idea to use names like mystring. How do you know that the request contains anything called mystring? How do you know it is not MYSTRING or MyString?

And there is no such word as urgent in our vocabulary.
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2229
    
    7

Ziya Bakan wrote:I assigned a value into the quotes like "0.5" to mystring, and it works...It seems like that the value of mystring isnt passed to parseDouble()..Any ideas why ?


If you hard-coded the mystring works, but your code uses the request to get this mystring. Can the request get a null back?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37884
    
  22
Of course you can get null from such a call. If there is no such column in the database or request, or if the value returns from the database as NULL, it will return null.
You should not use a String for such data in the first place. If you are supposed to have a double, put a double in the request. Actually, it is probably stored in your database as a DECIMAL, so return it as a BigDecimal rather than a double.
Jeyram Tamate
Greenhorn

Joined: Sep 29, 2012
Posts: 9
Internally server uses map to hold request parameters and
From your problem it seems that there is no value which is mapped with string mystring that is why it is returning null.
For debugging purpose you could check whether you are getting a not null value from request.getParameter("mystring");
Try to have some System.out.println() so that you can solve your problem.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

Campbell Ritchie wrote:Of course you can get null from such a call. If there is no such column in the database or request, or if the value returns from the database as NULL, it will return null.
You should not use a String for such data in the first place. If you are supposed to have a double, put a double in the request. Actually, it is probably stored in your database as a DECIMAL, so return it as a BigDecimal rather than a double.

I think request is a ServletRequest or HttpServletRequest, so that means that getParameter returns a String (or null). And if the return value is null, there simply isn't any parameter called mystring given. Period.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to solve error regarding string to double transforming?
 
Similar Threads
WAMP/Java/MysqlConnector Issue
sort TreeMap by value
Help with NullPointerException
java.lang.ClassCastException: java.lang.String cannot be cast to mycom.lang.MyString
SQLException on connect