NullPointerException on dialog.cancel()
<p>I have a dialog view with 2 buttons, Ok and Cancel. When the user clicks the cancel button I dismiss the dialog using <code>dialog.cancel()</code>. That works fine in the emulator and on my phones. However I am getting some users crash reports showing NullPointerException on the <code>dialog.cancel()</code> line. Does anyone see anything incorrect with the way I'm closing the dialog?</p>

<p>Code:</p>

<pre><code>@Override
public Dialog onCreateDialog(int id) {
    switch (id) {
        case (SHOW_DIALOG): {
            LayoutInflater li1 = LayoutInflater.from(this);
            View wView = li1.inflate(R.layout.dialog_w, null);
            AlertDialog.Builder wDialog = new AlertDialog.Builder(this);
            wDialog.setTitle("Dialog");
            wDialog.setIcon(android.R.drawable.ic_menu_add);
            wDialog.setView(wView);

            final DatePicker wDatePicker = (DatePicker) wView
                                    .findViewById(R.id.dialog_w_Date);
            final TextView DateTitleText = (TextView) wView
                                    .findViewById(R.id.dialog_w_Date_Title);
            final EditText wEditText = (EditText) wView
                                    .findViewById(R.id.dialog_w_w_EditText);
            final EditText bEditText = (EditText) wView
                                    .findViewById(R.id.dialog_w_b_EditText);

            wDialog.setPositiveButton("Ok",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        String wDateStr = "";
                        String wStr = "";
                        String bStr = "";
                        Calendar wDate = Calendar.getInstance();
                        wDate.set(wDatePicker.getYear(),
                        wDatePicker.getMonth(),
                        wDatePicker.getDayOfMonth());
                        wDateStr = DateUtil.CalendarToString(wDate);
                        wStr = wEditText.getText().toString();
                        bStr = bEditText.getText().toString();
                        if(wStr.length()&lt;1)
                            wStr = Double.toString(userObj.getW());
                        if(bStr.length()&lt;1)
                            bStr = Double.toString(userObj.getB());

                        mDbHelper.insertW(wDateStr,wStr,bStr);

                        updateFromSettings();
                        updateFromDB();
                        updateUI();
                        dialog.cancel();
                        }
                    });

            //NullPointerException Happening here
            wDialog.setNeutralButton("Cancel",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();
                        }
                    });
                    return wDialog.create();
            }
        }
        return null;
    }

@Override
public void onPrepareDialog(int id, Dialog dialog) {
    switch (id) {
        case (SHOW_DIALOG): {
            AlertDialog wDialog = (AlertDialog) dialog;
            DatePicker dp = (DatePicker) wDialog
                                    .findViewById(R.id.dialog_w_Date);
            EditText w = (EditText) wDialog
                                    .findViewById(R.id.dialog_w_w_EditText);
            EditText b = (EditText) wDialog
                                    .findViewById(R.id.dialog_w_b_EditText);

            Calendar c = Calendar.getInstance();
            int cYear = c.get(Calendar.YEAR);
            int cMonth = c.get(Calendar.MONTH);
            int cDay = c.get(Calendar.DAY_OF_MONTH);
            dp.init(cYear, cMonth, cDay, null);
            w.setText("");
            b.setText("");

            w.setHint(Double.toString(userObj.getW()));
            b.setHint(Double.toString(userObj.getB()));

            break;
            }
        }
    }
</code></pre>

<p>Here is the StackTrace:</p>

<pre><code>java.lang.RuntimeException: Unable to start activity ComponentInfo{com.woodsy.appname/com.woodsy.appname.UserActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1872)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1893)
at android.app.ActivityThread.access$1500(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1054)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:4385)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.woodsy.appname.UserActivity.onCreateDialog(UserActivity.java:269)
at android.app.Activity.onCreateDialog(Activity.java:2643)
at android.app.Activity.createDialog(Activity.java:924)
at android.app.Activity.restoreManagedDialogs(Activity.java:913)
at android.app.Activity.performRestoreInstanceState(Activity.java:859)
at android.app.Instrumentation.callActivityOnRestoreInstanceState(Instrumentation.java:1138)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1850)
... 11 more
</code></pre>
