-3

How to convert a string to double?

MainActivity.java

Location savedLocation = new Location("databaseLocation");
                savedLocation.setLatitude(Double.parseDouble(latitude));

Logcat:

07-06 14:41:21.870: E/AndroidRuntime(24097): FATAL EXCEPTION: main
07-06 14:41:21.870: E/AndroidRuntime(24097): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.locademo/com.example.locademo.MainActivity}: java.lang.NumberFormatException: Invalid double: ""
07-06 14:41:21.870: E/AndroidRuntime(24097):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
07-06 14:41:21.870: E/AndroidRuntime(24097):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2146)
07-06 14:41:21.870: E/AndroidRuntime(24097):    at android.app.ActivityThread.access$700(ActivityThread.java:140)
07-06 14:41:21.870: E/AndroidRuntime(24097):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1238)
07-06 14:41:21.870: E/AndroidRuntime(24097):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-06 14:41:21.870: E/AndroidRuntime(24097):    at android.os.Looper.loop(Looper.java:177)
07-06 14:41:21.870: E/AndroidRuntime(24097):    at android.app.ActivityThread.main(ActivityThread.java:4947)
07-06 14:41:21.870: E/AndroidRuntime(24097):    at java.lang.reflect.Method.invokeNative(Native Method)
07-06 14:41:21.870: E/AndroidRuntime(24097):    at java.lang.reflect.Method.invoke(Method.java:511)
07-06 14:41:21.870: E/AndroidRuntime(24097):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
07-06 14:41:21.870: E/AndroidRuntime(24097):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
07-06 14:41:21.870: E/AndroidRuntime(24097):    at dalvik.system.NativeStart.main(Native Method)
07-06 14:41:21.870: E/AndroidRuntime(24097): Caused by: java.lang.NumberFormatException: Invalid double: ""
07-06 14:41:21.870: E/AndroidRuntime(24097):    at java.lang.StringToReal.invalidReal(StringToReal.java:63)
07-06 14:41:21.870: E/AndroidRuntime(24097):    at java.lang.StringToReal.parseDouble(StringToReal.java:248)
07-06 14:41:21.870: E/AndroidRuntime(24097):    at java.lang.Double.parseDouble(Double.java:295)
07-06 14:41:21.870: E/AndroidRuntime(24097):    at com.example.locademo.MainActivity.getExpandableListData(MainActivity.java:203)
07-06 14:41:21.870: E/AndroidRuntime(24097):    at com.example.locademo.MainActivity.onCreate(MainActivity.java:107)
07-06 14:41:21.870: E/AndroidRuntime(24097):    at android.app.Activity.performCreate(Activity.java:5207)
07-06 14:41:21.870: E/AndroidRuntime(24097):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
07-06 14:41:21.870: E/AndroidRuntime(24097):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2085)
07-06 14:41:21.870: E/AndroidRuntime(24097):    ... 11 more
davidgiga1993
  • 2,695
  • 18
  • 30
Manish
  • 83
  • 1
  • 9

3 Answers3

3

Error clearly shows your value is empty

java.lang.NumberFormatException: Invalid double: ""

You need add one condition like this

if (!latitude.equals("")){
Location savedLocation = new Location("databaseLocation");
                savedLocation.setLatitude(Double.parseDouble(latitude));
}

Note : Whenever you make number format conversion make sure you don't get any empty or null values

Amsheer
  • 7,046
  • 8
  • 47
  • 81
1

Double.parseDouble(String) is working fine, but throws an NumberFormatException if the supplied value cannot be converted to a double. Referring to the second line of your stacktrace:

java.lang.NumberFormatException: Invalid double: ""

means in this case latitude had the value "" (empty string) and the conversion failed. You may implement some checks for blank strings (empty string or null), fix the value of latitude before it reaches the conversion or catch the exception and handle it.

Binkan Salaryman
  • 3,008
  • 1
  • 17
  • 29
-1

Check that youR latitude value will be null or blank.

if (!latitude.equals("")){
// apply your logic
}
Samir Bhatt
  • 3,041
  • 2
  • 25
  • 39