-1

In my Android app I have this code:

LatLng[] branches;
String[] branchesArray = HomeActivity.branches.toArray(new String[HomeActivity.branches.size()]);

for (int i = 0; i < HomeActivity.branches.size(); i++) {
    branches[i] = getLocationFromAddress(branchesArray[i]);
}

getLocationFromAddress method:

public LatLng getLocationFromAddress(String strAddress) {

    Geocoder coder = new Geocoder(this);
    List<Address> address;
    LatLng p1 = null;

    try {
        address = coder.getFromLocationName(strAddress, 1);

        if (address == null) {
            return null;
        }

        Address location = address.get(0);
        location.getLatitude();
        location.getLongitude();

        p1 = new LatLng((double) (location.getLatitude()), (double) (location.getLongitude()));
    } catch (IOException e) {
        Log.e("Error", e.getMessage());
    }

    return p1;
}

This code supposed to create an array of LatLng, extracted from an array of string addresses. The problem is that whenever I'm running this code, I get java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0 in the log. It refers to line 137 as the problematic line, which is this line:

Address location = address.get(0);

How can I fix that?

Ido
  • 171
  • 2
  • 13

2 Answers2

0

getLocationFromName documentation says:

returns a list of Address objects. Returns null or empty list if no matches were found or there is no backend service available.

In your case it is returning an empty list, so you should add an additional check:

public LatLng getLocationFromAddress(String strAddress) {

    Geocoder coder = new Geocoder(this);
    List<Address> address;
    LatLng p1 = null;

    try {
        address = coder.getFromLocationName(strAddress, 1);

        if (address == null || address.isEmpty()) {
            return null;
        }

        Address location = address.get(0);

        p1 = new LatLng(location.getLatitude(), location.getLongitude());
    } catch (IOException e) {
        Log.e("Error", e.getMessage());
    }

    return p1;
}
Juan Cruz Soler
  • 8,172
  • 5
  • 41
  • 44
  • The problem is that I don't need it to return null. I need it to return a `LatLng` object. – Ido Jan 16 '17 at 18:48
0

The probleme is you are forgetting to initialize "branches" variable with the correct size that's why your are getting "size 0 index 0"

String[] branches = HomeActivity.branches.toArray(new String[HomeActivity.branches.size()]);
thunder413
  • 585
  • 3
  • 10
  • I didn't understand your answer. You just copied the line from my code but changed `branchesArray` to `branches`. What's the meaning? – Ido Jan 16 '17 at 18:47
  • your loop goes above index 0 and as you initialized String[] branches; your like saying i'm creating an empty array witch size is 0 Arrays are not like List with which you can append indeterminately items without knowing the size ahead. Array in the other hand must be created with a defined size so String branches[] is an array size 0 (empty) and String[] branches = new String[20] is an array that can hold 20 items without having indexOutOfBound issues – thunder413 Jan 16 '17 at 19:22
  • that's why when you loop reach 1 or (in your case i guess 0) branches[1] = getLocationFromAddress(branchesArray[1]); It throw an error because you did tell that the maximum length of you branch var is 1 or even 0 ) – thunder413 Jan 16 '17 at 19:25