1

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) 
Meet
  • 441
  • 4
  • 15
  • Can you post the full trace of the crash please ? I suppose it's your eSignResponse or responseURL variable that is null – Bubu Aug 30 '18 at 13:01
  • 1
    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) – Srikar Reddy Aug 30 '18 at 13:02
  • show the exception. also you don't need to do manual dispatch to the fragment if you use fragment#startActivityForResult – nikis Aug 30 '18 at 13:05
  • You should also briefly explain things like "Aadhar" which seems to be a misspelled name for some sort of Indian identity thing. – James Z Aug 30 '18 at 13:06
  • Please check the exception i have edited in my question. – Meet Aug 30 '18 at 13:10
  • Inside the for loop `fragment` is null -> `null.onActivityResult` – Srikar Reddy Aug 30 '18 at 14:17
  • What are you trying to do? Pass the result received in the Activity `onActivityResult` to Fragment `onActivityResult`? – Srikar Reddy Aug 30 '18 at 14:19
  • Yeah exactly that's what i'm trying to do. – Meet Sep 01 '18 at 05:58

2 Answers2

2

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);
        }
    }
}
Meet
  • 441
  • 4
  • 15
0

Per exception you've posted you have null item in the collection returned by getFragments() call. The reason why do you have it there is for another question, but in the meantime I advise you to use Fragment#startActivityForResult so that result is automatically dispatched to the correct fragment after.

nikis
  • 10,833
  • 2
  • 32
  • 45