0

I am accepting address by user and then trying to get latitude and longitude for that i am using AsyncTask.

Every time i am getting : RuntimeException: An error occured while executing doInBackground()

And facing exception at this line: Address fetchedAddress = addresses.get(0);

Log:

E/AndroidRuntime(810): FATAL EXCEPTION: AsyncTask #2
E/AndroidRuntime(810): Process: app.android.fields, PID: 810
E/AndroidRuntime(810): java.lang.RuntimeException: An error occured while executing doInBackground()
E/AndroidRuntime(810):  at android.os.AsyncTask$3.done(AsyncTask.java:300)
E/AndroidRuntime(810):  at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
E/AndroidRuntime(810):  at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
E/AndroidRuntime(810):  at java.util.concurrent.FutureTask.run(FutureTask.java:242)
E/AndroidRuntime(810):  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
E/AndroidRuntime(810):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
E/AndroidRuntime(810):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
E/AndroidRuntime(810):  at java.lang.Thread.run(Thread.java:841)
E/AndroidRuntime(810): Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
E/AndroidRuntime(810):  at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
E/AndroidRuntime(810):  at java.util.ArrayList.get(ArrayList.java:308)
E/AndroidRuntime(810):  at app.android.users.MainActivity$RegisterUser.doInBackground(MainActivity.java:326)
E/AndroidRuntime(810):  at app.android.users.MainActivity$RegisterUser.doInBackground(MainActivity.java:1)
E/AndroidRuntime(810):  at android.os.AsyncTask$2.call(AsyncTask.java:288)
E/AndroidRuntime(810):  at java.util.concurrent.FutureTask.run(FutureTask.java:237)
E/AndroidRuntime(810):  ... 4 more

Code:

@Override
            protected String doInBackground(String... args) {

                ............

                strNewClientCompleteAddress = 
                    strNewClientAddress+" "+strNewClientCity+" "+" "+strNewClientState+" "+strNewClientPostalCode;
                Log.v("strNewClientCompleteAddress:",strNewClientCompleteAddress);


                Geocoder geocoder= new Geocoder(MainActivity.this);

                try {                                                
                    List<Address> addresses = geocoder.getFromLocationName(strNewClientCompleteAddress, 1);                           
                    if(addresses != null) {                            
                        Address fetchedAddress = addresses.get(0);
                        lat = fetchedAddress.getLatitude();
                        lng = fetchedAddress.getLongitude();
                        Log.v("try-if", "ok great work");                       
                    }
                    else {                      
                        Log.v("try-else", "something wrong");                       
                    }

                    }
                    catch (IOException e) {
                             // TODO Auto-generated catch block
                        e.printStackTrace();                                    
                        Log.v("catch", "Could not get address....!");                       
                    }                           

                strLat = String.valueOf(lat);
                Log.v("lat:", strLat);
                strLng = String.valueOf(lng);
                Log.v("lng:", strLng);

               ........................
           }

Updated code:

..............
if((addresses != null) && (addresses.size() > 0)) {                
    Address fetchedAddress = addresses.get(0);
    lat = fetchedAddress.getLatitude();
    lng = fetchedAddress.getLongitude();
    Log.v("try-if", "ok great work");                       
}
..............

Log:

V/strNewClientCompleteAddress:(1119): 1701 Amphitheatre Pkwy Mountain View  CA 94043
V/try-else(1119): something wrong
V/lat:(1119): 0.0
V/lng:(1119): 0.0
6
  • If your strNewClientCompleteAddress is not valid then addresses might be NULL. and remember one thing .getFromLocationName() not return Location all time. Commented Feb 18, 2015 at 6:27
  • your logcat clearly indicating--> Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0 First check the address.size()>0 then access it. Commented Feb 18, 2015 at 6:33
  • I posted an answer to a similar question, stackoverflow.com/questions/28532520 where the issue was the url needed to start with http:// and the app needed INTERNET use-permission Commented Feb 18, 2015 at 6:36
  • @Rustam now i am not getting any exception and also unable to get latitude and longitude, i have posted updated code and log please check Commented Feb 18, 2015 at 6:58
  • @Sun You should use either addresses.isEmpty() == false or addresses.size() > 0. Both does the same. Commented Feb 18, 2015 at 7:02

2 Answers 2

2

try to check addresses.size() > 0

 if ((addresses != null) && (addresses.size() > 0)) {
   Address address=addresses.get(0);
 }
Sign up to request clarification or add additional context in comments.

3 Comments

now i am not getting any exception and also unable to get latitude and longitude
check your strNewClientCompleteAddress is valid or not.
i posted updated code and log as well please check above, and getting V/strNewClientCompleteAddress:(1119): 1701 Amphitheatre Pkwy Mountain View CA 94043
1

the addresse array size is zero.it means your address array contains no values and you are trying to get the first position of the array value.

before get the value from array need to check null and size like below

 if(addresses!=null && addresses.size()>0)
 { 
 Address fetchedAddress = addresses.get(0);
 }else
  {
       return yourcondition; 
   }

2 Comments

now i am not getting any exception and also unable to get latitude and longitude, i have posted updated code and log please check
if you need to access current location add this permission to your menifest and try once <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.