Search code examples
androidbiometrics

Getting NullPointerException at android.support.v4.app.Fragment.onActivityResult(int, int, android.content.Intent)


I am using the NSDL Esign portal for to authenticate the Aadhar details with the Biometric device. Fingerprint data is scanned successfully but unfortunately at the end of the process my application is getting crashed with above error. This is what i have implemented in the Activity

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    for (Fragment fragment : getSupportFragmentManager().getFragments()) {
        fragment.onActivityResult(requestCode, resultCode, data);
    }
}

And here is the code i have added in the Fragment, to add the fingerprint data into my server.

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
        case Constants.NSDL_REQUEST_CODE:
            if (resultCode == RESULT_OK && null != data) {
                String eSignResponse = data.getStringExtra("signedResponse");
                try {
                    String postData = "msg=" + URLEncoder.encode(eSignResponse, "UTF-8") + "&type=M";
                    webView.postUrl(responseURL, postData.getBytes("UTF-8"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
            break;
    }
}

Error occurred in LogCat is

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100, result=-1, data=Intent { (has extras) }} to activity {com.fin.esign/com.fin.esign.BaseActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.app.Fragment.onActivityResult(int, int, android.content.Intent)' on a null object reference
                                                             at android.app.ActivityThread.deliverResults(ActivityThread.java:4255)
                                                             at android.app.ActivityThread.handleSendResult(ActivityThread.java:4298)
                                                             at android.app.ActivityThread.-wrap20(ActivityThread.java)
                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1613)
                                                             at android.os.Handler.dispatchMessage(Handler.java:110)
                                                             at android.os.Looper.loop(Looper.java:203)
                                                             at android.app.ActivityThread.main(ActivityThread.java:6339)
                                                             at java.lang.reflect.Method.invoke(Native Method)
                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1084)
                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:945)
                                                          Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.app.Fragment.onActivityResult(int, int, android.content.Intent)' on a null object reference
                                                             at com.fin.esign.BaseActivity.onActivityResult(BaseActivity.java:122)
                                                             at android.app.Activity.dispatchActivityResult(Activity.java:6948)
                                                             at android.app.ActivityThread.deliverResults(ActivityThread.java:4251)
                                                             at android.app.ActivityThread.handleSendResult(ActivityThread.java:4298) 
                                                             at android.app.ActivityThread.-wrap20(ActivityThread.java) 
                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1613) 
                                                             at android.os.Handler.dispatchMessage(Handler.java:110) 
                                                             at android.os.Looper.loop(Looper.java:203) 
                                                             at android.app.ActivityThread.main(ActivityThread.java:6339) 
                                                             at java.lang.reflect.Method.invoke(Native Method) 
                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1084) 
                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:945) 

Solution

  • I solved the issue by adding removing the for loop for fragments and simply add my specific fragment where i was willing to receive the result.

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        for (Fragment fragment : getSupportFragmentManager().getFragments()) {
            if (null != fragment) {
                fragment.onActivityResult(requestCode, resultCode, data);
            } else {
                new MyFragment().onActivityResult(requestCode, resultCode, data);
            }
        }
    }