Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to solve error regarding string to double transforming?

 
Ziya Bakan
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3443
13
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ziya Bakan
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48943
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3443
13
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48943
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic