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)