You need to check if the cursor is empty or not.
private String getPath(Uri selectedImage) {
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = context.getContentResolver().query(
selectedImage, filePathColumn, null, null, null);
String filePath = "";
int columnIndex = 0;
if (cursor.moveToFirst()) {
columnIndex = cursor.getColumnIndex(filePathColumn[0]);
filePath = cursor.getString(columnIndex);
cursor.close();}
}