I have written a for loop to add multiple geofences on a Google Map, but it is giving me IndexOutOfBoundsException: Invalid index 3, size is 3. But it is impossible to have this issue since my condition is saying radius< list.size(), which is 3. But when I put in Log and check line by line, I found that inside onSuccess method, radius is returning a value of 3, which cause this problem.
Code:
int permission = ContextCompat.checkSelfPermission(getContext(),
Manifest.permission.ACCESS_FINE_LOCATION);
if (permission == PackageManager.PERMISSION_GRANTED) {
for(count = 0; count < id_list.size(); count++) {
Log.d(TAG, "init: " + count);
LatLng latlng = new LatLng(lat_list.get(count), lng_list.get(count));
latlng_list.add(latlng);
}
Log.d(TAG, "latlng_list size: "+ latlng_list.size());
for(radius=0; radius<latlng_list.size(); radius++) {
// Add geofence
// passing all the data inside the list into GeofenceHelper class for process
Geofence geofence = geofenceHelper.getGeofence(id_list.get(radius), lat_list.get(radius), lng_list.get(radius), radius_list.get(radius), Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_DWELL | Geofence.GEOFENCE_TRANSITION_EXIT);
GeofencingRequest request = geofenceHelper.getGeofencingRequest(geofence);
PendingIntent pendingIntent = geofenceHelper.getPendingIntent();
Log.e("middle", String.valueOf(radius));
geofencingClient.addGeofences(request, pendingIntent)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
// Here is returning 3
Log.d(TAG, "onSuccess: " + radius);
addCircle(latlng_list.get(radius), radius_list.get(radius));
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
String errorMessage = geofenceHelper.getErrorString(e);
Toast.makeText(getContext(), errorMessage, Toast.LENGTH_SHORT).show();
}
});
Log.d("TAG", "here");
}
}
Logcat:`
11-18 12:15:45.157 1282-1282/com.example.assignment E/id_list: 3
11-18 12:15:45.157 1282-1282/com.example.assignment E/lat_list: 3
11-18 12:15:45.157 1282-1282/com.example.assignment E/lng_list: 3
11-18 12:15:45.157 1282-1282/com.example.assignment E/radius_list: 3
11-18 12:15:45.157 1282-1282/com.example.assignment D/MapsFragment: init: 0
11-18 12:15:45.157 1282-1282/com.example.assignment D/MapsFragment: init: 1
11-18 12:15:45.157 1282-1282/com.example.assignment D/MapsFragment: init: 2
11-18 12:15:45.157 1282-1282/com.example.assignment D/MapsFragment: latlng_list size: 3
11-18 12:15:45.158 1282-1282/com.example.assignment E/b4: 0
11-18 12:15:45.160 1282-1282/com.example.assignment E/middle: 0
11-18 12:15:45.168 1282-1282/com.example.assignment D/TAG: here
11-18 12:15:45.168 1282-1282/com.example.assignment E/b4: 1
11-18 12:15:45.168 1282-1282/com.example.assignment E/middle: 1
11-18 12:15:45.168 1282-1282/com.example.assignment D/TAG: here
11-18 12:15:45.168 1282-1282/com.example.assignment E/b4: 2
11-18 12:15:45.168 1282-1282/com.example.assignment E/middle: 2
11-18 12:15:45.169 1282-1282/com.example.assignment D/TAG: here
11-18 12:15:45.182 1282-1282/com.example.assignment D/MapsFragment: onSuccess: 3
Error code:
11-18 12:15:45.187 1282-1282/com.example.assignment E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.assignment, PID: 1282
java.lang.IndexOutOfBoundsException: Invalid index 3, size is 3
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
at com.example.assignment.fragments.MapsFragment$6.onSuccess(MapsFragment.java:429)
at com.example.assignment.fragments.MapsFragment$6.onSuccess(MapsFragment.java:424)
at com.google.android.gms.tasks.zzm.run(com.google.android.gms:play-services-tasks@@17.0.2:4)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at com.google.android.gms.internal.tasks.zzb.dispatchMessage(com.google.android.gms:play-
services-tasks@@17.0.2:6)
at android.os.Looper.loop(Looper.java:179)
at android.app.ActivityThread.main(ActivityThread.java:5650)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:681)