Preview captured Image NullpointerException
<pre><code>public void openDialogToAddReminder(final Context context, final DbHelper dbHelper, final int Rem_id, final int Med_id) {
    LayoutInflater layoutInflaterAndroid = LayoutInflater.from(context);
    final View mView = layoutInflaterAndroid.inflate(R.layout.add_reminders_dialog, null);
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(new ContextThemeWrapper(context, R.style.myDialog));
    alertDialogBuilder.setView(mView);
    captureImage = (ImageButton) mView.findViewById(R.id.capture_image);
    captureImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            selectImage(context);
        }
    });
    alertDialogBuilder
            .setCancelable(false)
            .setPositiveButton(dialog_title, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                }
            })
            .setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });
    AlertDialog alertDialog = alertDialogBuilder.create();
    // show it
    alertDialog.show();
}
public void selectImage(final Context context) {
    final CharSequence[] items = { "Take Photo", "Choose from Gallery",
            "Cancel" };
    android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(context);
    builder.setTitle("Add Photo");
    builder.setItems(items, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int item) {
            boolean result= Utility.checkPermission(context);
            if (items[item].equals("Take Photo")) {
                userChoosenTask ="Take Photo";
                if(result)
                    cameraIntent(context);
            } else if (items[item].equals("Choose from Gallery")) {
                userChoosenTask ="Choose from Gallery";
                if(result)
                    galleryIntent(context);
            } else if (items[item].equals("Cancel")) {
                dialog.dismiss();
            }
        }
    });
    builder.show();
}
public void galleryIntent(Context context)
{
    Log.i("Context ",context.toString());
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);//
    startActivityForResult(Intent.createChooser(intent, "Select File"),SELECT_FILE);
}
public void cameraIntent(Context context)
{
    Intent takingPictureCameraintent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takingPictureCameraintent.resolveActivity(context.getPackageManager())!=null)
        startActivityForResult(takingPictureCameraintent, REQUEST_CAMERA);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == SELECT_FILE)
            onSelectFromGalleryResult(data);
        else if (requestCode == REQUEST_CAMERA)
            onCaptureImageResult(data);
    }
}
public void onCaptureImageResult(Intent data)
{
    try{
        Bundle extras=data.getExtras();
        Bitmap thumbnail = (Bitmap) extras.get("data");
        Log.i("Image Camera Bitmap ",thumbnail.toString());
        ByteArrayOutputStream bytes=new ByteArrayOutputStream();
        thumbnail.compress(Bitmap.CompressFormat.JPEG, 90,bytes);
        captureImage.setImageBitmap(thumbnail);
        saveToGallery(thumbnail);
    }
    catch (Exception e){e.printStackTrace();}
}
</code></pre>

<h2>add_reminders_dialog.xml</h2>

<pre><code>&lt;?xml version = "1.0" encoding="utf-8"?&gt;
&lt;RelativeLayout android:layout_marginLeft="@dimen/activity_horizontal_margin"
                android:layout_marginRight="@dimen/activity_vertical_margin"
                android:layout_marginTop="@dimen/activity_horizontal_margin"
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/r1"
                xmlns:app="http://schemas.android.com/apk/res-auto"&gt;

    &lt;android.support.v7.widget.CardView
            android:id="@+id/cardview_medicine_image"
            android:layout_marginTop="20dp"
            app:contentPadding="@dimen/activity_horizontal_margin"
            android:layout_below="@+id/cardview_medicine_time"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"&gt;

    &lt;ImageButton
            android:layout_width="match_parent"
            android:layout_height="170dp"
            android:background="#424242"
            android:padding="10dp"
            android:clickable="true"
            android:src="@drawable/icon_camera"
            android:scaleType="fitCenter"
            android:id="@+id/capture_image"/&gt;
    &lt;/android.support.v7.widget.CardView&gt;
&lt;/RelativeLayout&gt;
</code></pre>

<p>Getting <strong>NullPointerException</strong> here:</p>

<pre><code>captureImage.setImageBitmap(thumbnail);
</code></pre>

<p>I don't know why I am getting captureImage null. As I have define it globally.  Why it is null as I have defined it inside the dialog box.</p>