Failure delivering result ResultInfo{who=null, request=100, result=-1, data=Intent

varmashrivastava picture varmashrivastava · Aug 22, 2018 · Viewed 7.1k times · Source

I am getting below exception rarely while selecting video from gallery-

Fatal Exception: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100, result=-1, data=Intent { dat=content://com.estrongs.files/storage/emulated/0/ADM/720P_1500K_152999072.mp }} to activity {xyzMainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.contains(java.lang.CharSequence)' on a null object reference
       at android.app.ActivityThread.deliverResults(ActivityThread.java)
       at android.app.ActivityThread.handleSendResult(ActivityThread.java)
       at android.app.ActivityThread.access$1500(ActivityThread.java)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java)
       at android.os.Handler.dispatchMessage(Handler.java)
       at android.os.Looper.loop(Looper.java)
       at android.app.ActivityThread.main(ActivityThread.java)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
       at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:102)

Caused by java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.contains(java.lang.CharSequence)' on a null object reference
       at android.content.ContentResolver.checkLeakDetectIgnoreList(ContentResolver.java)
       at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java)
       at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java)
       at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java)
       at android.media.MediaPlayer.setDataSource(MediaPlayer.java)
       at android.widget.VideoView.openVideo(VideoView.java)
       at android.widget.VideoView.setVideoURI(VideoView.java)
       at android.widget.VideoView.setVideoURI(VideoView.java)
       at xyz.onActivityResult(Unknown Source)
       at android.app.Activity.dispatchActivityResult(Activity.java)
       at android.app.ActivityThread.deliverResults(ActivityThread.java)

Below is the code I am using:

private void uploadVideo() {
    try {
        Intent intent = new Intent();
        intent.setType("video/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent, "Select Video"), REQUEST_TAKE_GALLERY_VIDEO);
    } catch (Exception e) {

    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        if (requestCode == REQUEST_TAKE_GALLERY_VIDEO) {
            selectedVideoUri = data.getData();
            videoView.setVideoURI(selectedVideoUri);
            videoView.start();
        }
    }
}

I failed to understand why I am getting this exception as I am checking resultCode in onActivityResult before accessing data.

Answer

Mohammad Rbabah picture Mohammad Rbabah · Aug 22, 2018

try to change this

if (resultCode == RESULT_OK) {

to

 if (resultCode == RESULT_OK && data.getData()!=null) {