-4

I have a string array of addresses in my res/strings. xml. I'm just trying to loop through them and add a marker to google maps but I'm getting a OutOfBoundsException on my array. Here's the error...

             Process: com.gerhorgan.propertyprice, PID: 15147
             java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
             at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
             at java.util.ArrayList.get(ArrayList.java:308)
             at com.gerhorgan.propertyprice.MapsActivity.onMapReady(MapsActivity.java:83)
             at com.google.android.gms.maps.SupportMapFragment$zza$1.zza(Unknown Source)
             at com.google.android.gms.maps.internal.zzt$zza.onTransact(Unknown Source)
             at android.os.Binder.transact(Binder.java:380)
             at zu.a(:com.google.android.gms.DynamiteModulesB:82)
             at maps.ad.t$5.run(Unknown Source)
             at android.os.Handler.handleCallback(Handler.java:739)
             at android.os.Handler.dispatchMessage(Handler.java:95)
             at android.os.Looper.loop(Looper.java:135)
             at android.app.ActivityThread.main(ActivityThread.java:5910)
             at java.lang.reflect.Method.invoke(Native Method)
             at java.lang.reflect.Method.invoke(Method.java:372)
             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)

here's my code..

            package com.gerhorgan.propertyprice;

            import android.location.Address;
            import android.location.Geocoder;
            import android.os.Bundle;
            import android.support.v4.app.FragmentActivity;
            import android.view.View;
            import android.widget.ZoomControls;

            import com.google.android.gms.maps.CameraUpdateFactory;
            import com.google.android.gms.maps.GoogleMap;
            import com.google.android.gms.maps.OnMapReadyCallback;
            import com.google.android.gms.maps.SupportMapFragment;
            import com.google.android.gms.maps.model.LatLng;
            import com.google.android.gms.maps.model.MarkerOptions;

            import java.io.IOException;
            import java.util.List;

            public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

                private GoogleMap mMap;
                ZoomControls zoom;
                private String[] addressArray;


                @Override
                protected void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.activity_maps);
                    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
                    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                            .findFragmentById(R.id.map);
                    mapFragment.getMapAsync(this);

                    zoom = (ZoomControls) findViewById(R.id.zcZoom);

                    zoom.setOnZoomOutClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            mMap.animateCamera(CameraUpdateFactory.zoomOut());

                        }
                    });

                    zoom.setOnZoomInClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {

                            mMap.animateCamera(CameraUpdateFactory.zoomIn());

                        }
                    });

                }


                /**
                 * Manipulates the map once available.
                 * This callback is triggered when the map is ready to be used.
                 * This is where we can add markers or lines, add listeners or move the camera. In this case,
                 * we just add a marker near Sydney, Australia.
                 * If Google Play services is not installed on the device, the user will be prompted to install
                 * it inside the SupportMapFragment. This method will only be triggered once the user has
                 * installed Google Play services and returned to the app.
                 */
                @Override
                public void onMapReady(GoogleMap googleMap) {
                    mMap = googleMap;


                    addressArray= getResources().getStringArray(R.array.addresses);
                    Geocoder geocoder= new Geocoder(MapsActivity.this);
                    List<Address> addressList= null;
                    for(int i=0; i< addressArray.length; i++)

                    {
                        try {
                            addressList= geocoder.getFromLocationName(addressArray[i],1);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        Address address= addressList.get(0);
                        LatLng latlng= new LatLng(address.getLatitude(), address.getLongitude());
                        mMap.addMarker(new MarkerOptions().position(latlng).title(addressArray[i]));
                        mMap.animateCamera(CameraUpdateFactory.newLatLng(latlng));


                    }






                }
            }
flutter
  • 6,188
  • 9
  • 45
  • 78

6 Answers6

4

Your addressList is empty. So when you try to do addressList.get(0); you are trying to get the first element of an empty List. Then you will get that exception.

JavaBoy
  • 182
  • 7
1

you only have to make sure if the array is null and if the size is greater than zero so I think you only have to to this:

if(addressList != null && addressList.size() > 0) 
{

   Address address= addressList.get(0);
   LatLng latlng= new LatLng(address.getLatitude(), address.getLongitude());
   mMap.addMarker(new MarkerOptions().position(latlng).title(addressArray[i]));
   mMap.animateCamera(CameraUpdateFactory.newLatLng(latlng));
} 
Francisco Castro
  • 435
  • 10
  • 20
  • thanks that did the trick. Jus don't understand why the array doesn't populate on the first iteration but always good to do some checking. – flutter Jan 11 '17 at 11:42
  • Sometimes the geocoder doenst work, I don't know what is on your string array but you could try get the information you need on the API check this link http://stackoverflow.com/questions/15182853/android-geocoder-getfromlocationname-always-returns-null – Francisco Castro Jan 11 '17 at 12:11
1

addressList is empty here. But trying get first element from the empty list which is throwing exception.

To avoid this, you can add a check

if(addressList.size() > 0) {
 Address address= addressList.get(0);
 //perform remaining
 }

Also, you check why geocoder.getFromLocationName(addressArray[i],1) is returning empty.

Raghu Nagaraju
  • 3,278
  • 1
  • 18
  • 25
0

The array list you are accessing is empty and its size is 0

addressList.get(0);

check the size of array list and access it.

As vishal mentioned you need to intialize the array list.

Also you need to use ArraList.add() to add a object to the array list.

addressList.add();

Also check whether you are getting values in this line,

addressList= geocoder.getFromLocationName(addressArray[i],1);
Balamurugan G
  • 21
  • 1
  • 7
0

In onMapReady method, you are not initializing your list.

List<Address> addressList= null;

replace your code with

List<Address> addressList= new ArrayList<>();
Abdul Waheed
  • 4,540
  • 6
  • 35
  • 58
  • This is not the answer. OP would get a `NullPointerException` instead. Read the code a bit further and you'll where that list is initialized. – Tom Jan 11 '17 at 11:58
0

This piece of code is giving you the exception.

Address address= addressList.get(0);

Do a slight modification in your code and it will work.

if(!addressList.isEmpty())
   Address address= addressList.get(0);
Kumar Utkarsh
  • 161
  • 1
  • 8