• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

java.lang.ArrayIndexOutOfBoundsException: length=0; index=0

I am facing some issue in my code, can anyone please help me on this, please find the below crash which i am getting,

Fatal Exception: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0

at com.***.in.***.onRequestPermissionsResult(***.java:476)

at android.app.Activity.requestPermissions(Activity.java:4290)

at com.***.in.***.startGeofence(***.java:450)

at com.***.in.***.FunctionCall(***.java:332)

at com.***.in.***.LoadConstants(***.java:238)

at com.***.in.***.access$100(***.java:75)

at com.***.in.***$5$1.run(***.java:267)

at android.os.Handler.handleCallback(Handler.java:754)

at android.os.Handler.dispatchMessage(Handler.java:95)

at android.os.Looper.loop(Looper.java:163)

at android.app.ActivityThread.main(ActivityThread.java:6393)

at java.lang.reflect.Method.invoke(Method.java)

at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:933)

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)


Please find my code where the crash is happening,

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(
new String[]{
Manifest.permission.ACCESS_FINE_LOCATION
},
4500);
}

@override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if(requestCode != RESULT_CANCELED) {
if (requestCode == 4500) {
***(grantResults[0] == PackageManager.PERMISSION_GRANTED); - #Crash in this line#
} else {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
}

Note: This crash is not happening in all devices, only some devices like (ViVo, Redmi, Oppo) - that too specific with specific devices and OS this crash occurs.
 
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if(requestCode != RESULT_CANCELED) {
if (requestCode == 4500) {
***(grantResults[0] == PackageManager.PERMISSION_GRANTED); - #Crash in this line#
} else {
Like Brian said, whatever is calling onRequestPermissionsResult is passing in an empty array. You need to track down what's setting it and figure out why it's empty.
 
Here is the documentation: https://developer.android.com/refer...vityCompat.OnRequestPermissionsResultCallback , and there is a note.
Note: It is possible that the permissions request interaction with the user is interrupted. In this case you will receive empty permissions and results arrays which should be treated as a cancellation.
So, first you should check the size/length of grantResults variable, if it is 0 it is considered as a cancellation of permissions request.
 
Back
Top Bottom