I'm trying to select photo from gallery through DialogFragment
. But I'm getting nullpointerexception
while initializing cursor
. Any ideas why getting this error?
Below is my code :
if (resultCode == Activity.RESULT_OK) {
Uri selectedImage = imageReturnedIntent.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
// Nullpointerexcepiton on this line
Cursor cursor = getActivity().getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
cursor.close();
}
Here is my logcat error :
03-24 12:34:37.645: E/AndroidRuntime(21479): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=65538, result=-1, data=Intent { dat=content://media/external/images/media/3890 flg=0x1 }} to activity {com.example/com.example.MainActivity}: java.lang.NullPointerException
03-24 12:34:37.645: E/AndroidRuntime(21479): at android.app.ActivityThread.deliverResults(ActivityThread.java:3462)
03-24 12:34:37.645: E/AndroidRuntime(21479): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3505)
03-24 12:34:37.645: E/AndroidRuntime(21479): at android.app.ActivityThread.access$1100(ActivityThread.java:150)
03-24 12:34:37.645: E/AndroidRuntime(21479): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1346)
03-24 12:34:37.645: E/AndroidRuntime(21479): at android.os.Handler.dispatchMessage(Handler.java:99)
03-24 12:34:37.645: E/AndroidRuntime(21479): at android.os.Looper.loop(Looper.java:213)
03-24 12:34:37.645: E/AndroidRuntime(21479): at android.app.ActivityThread.main(ActivityThread.java:5225)
03-24 12:34:37.645: E/AndroidRuntime(21479): at java.lang.reflect.Method.invokeNative(Native Method)
03-24 12:34:37.645: E/AndroidRuntime(21479): at java.lang.reflect.Method.invoke(Method.java:525)
03-24 12:34:37.645: E/AndroidRuntime(21479): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741)
03-24 12:34:37.645: E/AndroidRuntime(21479): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
03-24 12:34:37.645: E/AndroidRuntime(21479): at dalvik.system.NativeStart.main(Native Method)
03-24 12:34:37.645: E/AndroidRuntime(21479): Caused by: java.lang.NullPointerException
03-24 12:34:37.645: E/AndroidRuntime(21479): at com.example.MainDialogFragment.onActivityResult(MainDialogFragment.java:226)
03-24 12:34:37.645: E/AndroidRuntime(21479): at com.example.MainActivity.onActivityResult(DelictActivity.java:85)
03-24 12:34:37.645: E/AndroidRuntime(21479): at android.app.Activity.dispatchActivityResult(Activity.java:5322)
03-24 12:34:37.645: E/AndroidRuntime(21479): at android.app.ActivityThread.deliverResults(ActivityThread.java:3458)
03-24 12:34:37.645: E/AndroidRuntime(21479): ... 11 more