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
Sun
  • 6,768
  • 25
  • 76
  • 131
  • If your `strNewClientCompleteAddress` is not valid then addresses might be NULL. and remember one thing `.getFromLocationName()` not return Location all time. – M D Feb 18 '15 at 06: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. – Rustam Feb 18 '15 at 06:33
  • I posted an answer to a similar question, http://stackoverflow.com/questions/28532520 where the issue was the url needed to start with http:// and the app needed INTERNET use-permission – faljbour Feb 18 '15 at 06: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 – Sun Feb 18 '15 at 06:58
  • @Sun You should use either `addresses.isEmpty() == false` or `addresses.size() > 0`. Both does the same. – Pankaj Kumar Feb 18 '15 at 07:02
  • @PankajKumar yeah i removed addresses.isEmpty() == false – Sun Feb 18 '15 at 07:11

2 Answers2

2

try to check addresses.size() > 0

 if ((addresses != null) && (addresses.size() > 0)) {
   Address address=addresses.get(0);
 }
M D
  • 47,665
  • 9
  • 93
  • 114
  • now i am not getting any exception and also unable to get latitude and longitude – Sun Feb 18 '15 at 06:53
  • check your `strNewClientCompleteAddress` is valid or not. – M D Feb 18 '15 at 06:54
  • i posted updated code and log as well please check above, and getting V/strNewClientCompleteAddress:(1119): 1701 Amphitheatre Pkwy Mountain View CA 94043 – Sun Feb 18 '15 at 06:55
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; 
   }
RamBabu Pudari
  • 912
  • 7
  • 19
  • now i am not getting any exception and also unable to get latitude and longitude, i have posted updated code and log please check – Sun Feb 18 '15 at 06:58
  • if you need to access current location add this permission to your menifest and try once – RamBabu Pudari Feb 18 '15 at 07:08