Angelo Charl
Angelo Charl

Reputation: 345

"Attempt to get length of null array" on Permission run time

I am having some problem running my basic run time permission project.

Here is my code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //Initializing button
    buttonRequestPermission = (Button) findViewById(R.id.buttonRequestPermission);

    //Adding a click listener
    buttonRequestPermission.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //First checking if the app is already having the permission
            if (isReadStorageAllowed()) {
                //If permission is already having then showing the toast
                Toast.makeText(MainActivity.this, "You already have the permission", Toast.LENGTH_LONG).show();
                //Existing the method with return
                return;
            }

            //If the app has not the permission then asking for the permission
            requestStoragePermission();
        }
    });
}

//We are calling this method to check the permission status
private boolean isReadStorageAllowed() {
    //Getting the permission status
    int result = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE);

    //If permission is granted returning true
    if (result == PackageManager.PERMISSION_GRANTED)
        return true;

    //If permission is not granted returning false
    return false;
}
public void requestStoragePermission() {

    if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_EXTERNAL_STORAGE)) {

    }

    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, STORAGE_PERMISSION_CODE);
}

@Override
public void onRequestPermissionsResult(int requestCode,  String[] permissions,  int[] grantResults) {
    //Checking the request code of our request
    if (requestCode == STORAGE_PERMISSION_CODE) {

        //If permission is granted
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

            //Displaying a toast
            Toast.makeText(this, "Permission granted now you can read the storage", Toast.LENGTH_LONG).show();
        } else {
            //Displaying another toast if permission is not granted
            Toast.makeText(this, "Oops you just denied the permission", Toast.LENGTH_LONG).show();
        }
    }
}

In this function, the program seems to crash in the requestPermissions() function.

When I test the app on a device, the app crashes and returns "Unfortunately, Package installation has stopped". Then gives me this logcat error:

Process: com.google.android.packageinstaller, PID: 17894
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.google.android.packageinstaller/com.android.packageinstaller.permission.ui.GrantPermissionsActivity}: java.lang.NullPointerException: Attempt to get length of null array
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2417)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2477)
   at android.app.ActivityThread.access$900(ActivityThread.java:150)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
   at android.os.Handler.dispatchMessage(Handler.java:102)
   at android.os.Looper.loop(Looper.java:148)
   at android.app.ActivityThread.main(ActivityThread.java:5418)
   at java.lang.reflect.Method.invoke(Native Method)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to get length of null array
   at com.android.packageinstaller.permission.ui.GrantPermissionsActivity.computePermissionGrantState(GrantPermissionsActivity.java:312)
   at com.android.packageinstaller.permission.ui.GrantPermissionsActivity.updateDefaultResults(GrantPermissionsActivity.java:362)
   at com.android.packageinstaller.permission.ui.GrantPermissionsActivity.onCreate(GrantPermissionsActivity.java:105)
   at android.app.Activity.performCreate(Activity.java:6285)
   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2370)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2477) 
   at android.app.ActivityThread.access$900(ActivityThread.java:150) 
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
   at android.os.Handler.dispatchMessage(Handler.java:102) 
   at android.os.Looper.loop(Looper.java:148) 
   at android.app.ActivityThread.main(ActivityThread.java:5418) 
   at java.lang.reflect.Method.invoke(Native Method) 
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

I have also placed this into the manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example.angelos.permissiontest">
    <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">

        <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>

    </application>

</manifest>

I am not really sure why this is happening. This should be a super simple project. Can somebody help me?

Upvotes: 3

Views: 3294

Answers (3)

Angelo Charl
Angelo Charl

Reputation: 345

The answer here solved the problem as suggested by @greenapps and @Pankaj Kuma:

Putting the Permission tag outside of the application tag in the manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example.angelos.permissiontest">

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>

</application>

Reference : developer.android.com/training/permissions/declaring.html

Upvotes: 1

Pankaj Kumar
Pankaj Kumar

Reputation: 82948

This java.lang.NullPointerException is not because of your code. It is into com.android.packageinstaller.permission.ui.GrantPermissionsActivity.java. Below is code where you are getting java.lang.NullPointerException

309    private int computePermissionGrantState(PackageInfo callingPackageInfo,
310            String permission, int permissionPolicy) {
311        boolean permissionRequested = false;
312
313        for (int i = 0; i < callingPackageInfo.requestedPermissions.length; i++) {
314            .....
346    }

In you case, callingPackageInfo.requestedPermissions is null. So check if you declared requested permission into manifest correct.

Upvotes: 3

Sush
Sush

Reputation: 3874

public void requestStoragePermission() {

if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_EXTERNAL_STORAGE)) {
 // add the below line here
 ActivityCompat.requestPermissions(this, new String[]{


Manifest.permission.READ_EXTERNAL_STORAGE}, STORAGE_PERMISSION_CODE);
}

 // ActivityCompat.requestPermissions(this, new String[]    {Manifest.permission.READ_EXTERNAL_STORAGE}, STORAGE_PERMISSION_CODE);
}

make below changes to to avoid crash

if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)

replace with below line

if(grantResults != null && grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)

Upvotes: 0

Related Questions