0

I was trying to access showAlertForPermission from onRequestPermissionsResult which is in FilesActivity.java but it gives nullpointerexception at line

 Uri uri = Uri.fromParts("package", getPackageName(), null);

In ConnectionActivity.java :

public void showAlertForPermission (Context context){
        AlertDialog.Builder NeverPopUp =new AlertDialog.Builder(context);
        NeverPopUp.setMessage("To continue, allow \"iXm Uplink\" access to your device's audio files. Tap Settings > Permissions, and turn Storage on.")
                .setPositiveButton("Settings", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //settings
                        Intent intent = new Intent();
                        intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                        Uri uri = Uri.fromParts("package", getPackageName(), null);
                        intent.setData(uri);
                        startActivity(intent);
                    }
                })
                .setNegativeButton("Not Now", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //Exit
                    }
                })
                .show();
    }

In FilesActivity.java :

@Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case 1: {
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    Log.v("Storage: ","Storage permissions Granted");
                } else {
                    if (ActivityCompat.shouldShowRequestPermissionRationale(FilesActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                        //Shows permission explanation using dialog window
                    } else {
                        connectedActivity.showAlertForPermission(FilesActivity.this);

                    }
                }
            }
        }
    }

Error :

 E/AndroidRuntime: FATAL EXCEPTION: main        
                      java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
                          at android.content.ContextWrapper.getPackageName(ContextWrapper.java:132)
                          at android.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:157)
                          at android.os.Handler.dispatchMessage(Handler.java:102)
                          at android.os.Looper.loop(Looper.java:154)
                          at android.app.ActivityThread.main(ActivityThread.java:6077)
                          at java.lang.reflect.Method.invoke(Native Method)
                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

After some search i tried :

Intent intent = new Intent(activity, FileActivity.class);

error does exits, what i did wrong?

Abhinav Gupta
  • 2,225
  • 1
  • 14
  • 30
Sujay
  • 588
  • 6
  • 15
  • Your current `Context` is `null`. Debug your code. – ADM Mar 13 '18 at 07:24
  • what is connectectedActivity ? – Bawa Mar 13 '18 at 07:25
  • 2
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – AskNilesh Mar 13 '18 at 07:29
  • add only this code inside onClick() `startActivity(new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.parse("package:" + BuildConfig.APPLICATION_ID))); ` onclick it will directly take you to settings of your app. – Mohammed Farhan Mar 13 '18 at 07:47
  • @Bawa **Connectedactivity.java** is for checking connection of wifi and show the files in mobile...and if storage permission is not available then alert window is coming in there this working fine but when I am trying with internal storage using **FilesActivity.java** with same call (**showAlertForPermission**) it crash – Sujay Mar 13 '18 at 07:55
  • @MohammedFarhan its not working. – Sujay Mar 13 '18 at 08:29

3 Answers3

0

you might wanna try context.getPackageName().. context is the variable you passed to that method.

Bawa
  • 876
  • 10
  • 28
  • the thing is .. getPackageName requires a context to call it, in your case the context from which you are calling getPackageName is null. (you are calling the method inside an activity class which you have not even launched yet, directly calling getPackageName in that class and calling it, will leave you with a null context since the activity is not there yet.) – Bawa Mar 13 '18 at 07:30
  • i don't know, is the connectionactivity alive? – Bawa Mar 13 '18 at 07:31
  • After try context.getPackageName() following error is coming : **java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.ActivityThread$ApplicationThread android.app.ActivityThread.getApplicationThread()' on a null object reference** – Sujay Mar 13 '18 at 07:41
  • so your filesActivity.this is null, – Bawa Mar 13 '18 at 07:49
  • why dont you create your showAlertForPermission() method in the FilesActivity or make it a utilityMethod if you wanna reuse it again and again – Bawa Mar 13 '18 at 07:51
  • Yes i try but same error and then crash in connectedActivity – Sujay Mar 13 '18 at 08:15
0

This is because you're calling showAlertForPermission from ConnectionActivity.java class but you didn't create the ConnectionActivity object yet. It will definitely crashing your program by throwing a null pointer exception.

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    switch (requestCode) {
        case 1: {
          ...
          // This will throw error because you
          connectedActivity.showAlertForPermission(FilesActivity.this);

        }
    }
}

It seems to me that you are trying to make a reusable method to show the alert dialog. So, you can make the public showAlertForPermission like this:

public static void showAlertForPermission (Context context) {
  ...
}

then you can use it like this:

 ConnectionActivity.showAlertForPermission(FilesActivity.this);
ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
0

I prefer to use

BuildConfig.APPLICATION_ID

but first, assure your application Id is correct. because sometimes it is not referred to your application id and it may refer to library id or something else. if so just clean and rebuild your project again and assure you import the correct BuildConfig file because you may import a BuildConfig file of a library you use in the project

Mina Samir
  • 1,527
  • 14
  • 15