Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to prevent app from closing when Geocoder address list is null?
    text
    copied!<p>I am trying to handle errors for my application that uses Geocoder, but whenever i try to get the coordinates of a non-existent location my app crashes. I know this has been asked <a href="https://stackoverflow.com/questions/16733270/geocoder-not-putting-out-address">before</a> but i don't think it was resolved. Anyway i can prevent it from force closing, and just notify the user to input a correct string?</p> <p>Here is my code. a Sample string would be "qwihedhladhaw"</p> <pre><code>getCoordinatesFromString("qwihedhladhaw"); public double[] getCoordinatesFromString (String location) { Utils util = new Utils(); double coordinates[] = {0.0, 0.0}; if(util.checkIfEmptyString(location)) { this.canGetCoordinates = false; System.out.println("EMPTY STRINGS"); } else { Geocoder geocoder = new Geocoder(mContext); //geocoding List&lt;Address&gt; addressList; try { addressList = geocoder.getFromLocationName(location, 1, 14.479894, 120.970062, 14.774883, 121.061676 ); Address address = addressList.get(0); System.out.println(address); if(address.hasLatitude() &amp;&amp; address.hasLongitude()){ coordinates[0] = address.getLatitude(); coordinates[1] = address.getLongitude(); this.canGetCoordinates = true; this.isLocationValid = true; System.out.println(coordinates[0]); System.out.println(coordinates[1]); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return coordinates; } </code></pre> <p>Output logs are:</p> <pre><code> {07-30 23:12:50.291: I/System.out(26237): sDestination: 07-30 23:12:50.291: I/System.out(26237): sStart: qwert 07-30 23:12:50.526: D/AndroidRuntime(26237): Shutting down VM 07-30 23:12:50.531: W/dalvikvm(26237): threadid=1: thread exiting with uncaught exception (group=0x40c4a930) 07-30 23:12:50.566: E/AndroidRuntime(26237): FATAL EXCEPTION: main 07-30 23:12:50.566: E/AndroidRuntime(26237): java.lang.IllegalStateException: Could not execute method of the activity 07-30 23:12:50.566: E/AndroidRuntime(26237): at android.view.View$1.onClick(View.java:3599) 07-30 23:12:50.566: E/AndroidRuntime(26237): at android.view.View.performClick(View.java:4204) 07-30 23:12:50.566: E/AndroidRuntime(26237): at android.view.View$PerformClick.run(View.java:17360) 07-30 23:12:50.566: E/AndroidRuntime(26237): at android.os.Handler.handleCallback(Handler.java:725) 07-30 23:12:50.566: E/AndroidRuntime(26237): at android.os.Handler.dispatchMessage(Handler.java:92) 07-30 23:12:50.566: E/AndroidRuntime(26237): at android.os.Looper.loop(Looper.java:137) 07-30 23:12:50.566: E/AndroidRuntime(26237): at android.app.ActivityThread.main(ActivityThread.java:5233) 07-30 23:12:50.566: E/AndroidRuntime(26237): at java.lang.reflect.Method.invokeNative(Native Method) 07-30 23:12:50.566: E/AndroidRuntime(26237): at java.lang.reflect.Method.invoke(Method.java:511) 07-30 23:12:50.566: E/AndroidRuntime(26237): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:799) 07-30 23:12:50.566: E/AndroidRuntime(26237): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566) 07-30 23:12:50.566: E/AndroidRuntime(26237): at dalvik.system.NativeStart.main(Native Method) 07-30 23:12:50.566: E/AndroidRuntime(26237): Caused by: java.lang.reflect.InvocationTargetException 07-30 23:12:50.566: E/AndroidRuntime(26237): at java.lang.reflect.Method.invokeNative(Native Method) 07-30 23:12:50.566: E/AndroidRuntime(26237): at java.lang.reflect.Method.invoke(Method.java:511) 07-30 23:12:50.566: E/AndroidRuntime(26237): at android.view.View$1.onClick(View.java:3594) 07-30 23:12:50.566: E/AndroidRuntime(26237): ... 11 more 07-30 23:12:50.566: E/AndroidRuntime(26237): Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0 07-30 23:12:50.566: E/AndroidRuntime(26237): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251) 07-30 23:12:50.566: E/AndroidRuntime(26237): at java.util.ArrayList.get(ArrayList.java:304) 07-30 23:12:50.566: E/AndroidRuntime(26237): at com.example.otpxmlgetter.ReverseGeocode.getCoordinatesFromString(ReverseGeocode.java:33) 07-30 23:12:50.566: E/AndroidRuntime(26237): at com.example.otpxmlgetter.MainActivity.planTrip(MainActivity.java:136) 07-30 23:12:50.566: E/AndroidRuntime(26237): ... 14 more 07-30 23:17:50.721: I/Process(26237): Sending signal. PID: 26237 SIG: 9 } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload