-2

I have read all of the documentation on IOExceptions and what it means when they are produced. I am aware that I am searching for en Index in my ArrayList that doesn't exist, thus producing the IndexOutOfBoundsException: Index: 0, Size: 0. I have consulted the following documentation and posts to try to handle the problem on my own, but haven't been successful. After doing this thorough research I hope someone can help me elude this problem. I have legitimately looked through many posts of similar type problems on here to alone try to resolve my error, but haven't been able to come to the right conclusion.

What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?. https://docs.oracle.com/javase/tutorial/essential/exceptions/throwing.html

I have tried put en if statement around the addressList for example, and a toast message warning the user that they have to refine their search. When searching for most places on the map, the results come out fine and a marker is placed on the map at that specific location. When I search for Paris though, I get the exception and like I said I have wrapped it in an if statement and other methods, but no success.

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
    @Override
    public boolean onQueryTextSubmit(String query) {
        String location = searchView.getQuery().toString();
        List<Address> addressList = null;

        if (location != null || !location.equals("")) {
            Geocoder geocoder = new Geocoder(MapsActivityPublisher.this);
            try {
                addressList = geocoder.getFromLocationName(location, 1);
            } catch (IOException e) {
                e.printStackTrace();
            }
            Address address = addressList.get(0);
            LatLng latLng = new LatLng(address.getLatitude(), address.getLongitude());
            map.addMarker(new MarkerOptions().position(latLng).title(location).draggable(true));
            map.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 10));

        }

        return false;
    }

    @Override
    public boolean onQueryTextChange(String newText) {
        return false;
    }
});

Logcat

java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
        at java.util.ArrayList.get(ArrayList.java:437)
        at com.e.events.Map.MapsActivityPublisher$1.onQueryTextSubmit(MapsActivityPublisher.java:62)
        at android.widget.SearchView.onSubmitQuery(SearchView.java:1270)
        at android.widget.SearchView.access$1000(SearchView.java:99)
        at android.widget.SearchView$7.onEditorAction(SearchView.java:1247)
        at android.widget.TextView.onEditorAction(TextView.java:6453)
        at com.android.internal.widget.EditableInputConnection.performEditorAction(EditableInputConnection.java:138)
        at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:363)
        at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:85)
        at android.os.Handler.dispatchMessage(Handler.java:112)
        at android.os.Looper.loop(Looper.java:216)
        at android.app.ActivityThread.main(ActivityThread.java:7625)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987)
Miss Chanandler Bong
  • 4,081
  • 10
  • 26
  • 36
JMB
  • 313
  • 2
  • 9
  • *I have tried put en if statement around the addressList* - your code doesn't have anything of that sort – Thiyagu Dec 29 '19 at 12:21
  • @user7 I took it off because it didn't work... I did the ```if``` statement around ```Address address = …``` , but app kept crashing so I took it off – JMB Dec 29 '19 at 12:22
  • This or (`||`) statement should be an and (`&&`), since you want to check both cases: `if (location != null || !location.equals(""))`. Currently an empty string will pass the first check, so not be subjected to the second check. – Jake Lee Dec 29 '19 at 12:23
  • @JakeSteam just tried it... Still crashes – JMB Dec 29 '19 at 12:25
  • You need to show more of your code. `geocoder.getFromLocationName(location, 1);` is either throwing an error or returning an empty list. – Jake Lee Dec 29 '19 at 12:26
  • @JakeSteam well, that much I understand. But what can I do to fix it? The location isn't specific enough, so it comes back empty. How can I make it so that either the ```toast``` message shows and app doesn't crash so user knows to fix search or something else – JMB Dec 29 '19 at 12:29
  • @JMB Just add `if (addressList.size > 0) {` check around the 4 lines that handle the address? Can then add an `else` to do whatever you want. – Jake Lee Dec 29 '19 at 12:32
  • @JakeSteam okay man, seems to work. I did the same thing before, but substituting ```if (addressList != null) { … ```and then ```toasting``` it, but kept crashing. Think it's relatively similar resolution. But anyways, seems to work for now. Thanks! – JMB Dec 29 '19 at 12:44

1 Answers1

1

Check null and empty before get data from ArrayList

if (location != null && !location.equals("")) {
    Geocoder geocoder = new Geocoder(MapsActivityPublisher.this);
    try {
        addressList = geocoder.getFromLocationName(location, 1);
    } catch (IOException e) {
        e.printStackTrace();
    }

    if(addressList != null && !addressList.isEmpty()) {
        Address address = addressList.get(0);
        LatLng latLng = new LatLng(address.getLatitude(), address.getLongitude());
        map.addMarker(new MarkerOptions().position(latLng).title(location).draggable(true));
        map.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 10));
    }

}
Md. Asaduzzaman
  • 14,963
  • 2
  • 34
  • 46