• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps NullPointerException AlertDialog.Builder

Hi everyone :)

I've been looking into this for a couple of hours and changed the code quite a few times however the nullpointerexception error is still appearing

The problem is occuring when my "WorkOn" class calls another class called "Dialogs"

So this is my WorkOn Class
Java:
public Context context = this;
    Dialogs saveDialog = new Dialogs(context);

//constructor
    public WorkOn(Context context) {
    this.context = context;

}

    public void OnTime(View v) {


            saveDialog.WorkOnSavedDialog(v);


        }


    });


}

This is my Dialogs class

Java:
public class Dialogs extends AppCompatActivity  {




public Context context = this;
//constructor

public Dialogs(Context context) {
    this.context = context;


}

public void WorkOnSavedDialog(View v) {

    AlertDialog.Builder alert = new AlertDialog.Builder(Dialogs.this);


    alert.setTitle("Work On File Saved");
    alert.setMessage("[Displaying for Test] Work On File Saved");
    alert.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    // continue with delete
                }
            });
    alert.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    // do nothing
                }
            });
    alert.setIcon(android.R.drawable.ic_dialog_alert);
    alert.show();
}
}

The nullpointer excpection is happening because of

Java:
AlertDialog.Builder alert = new AlertDialog.Builder(Dialogs.this);

do you have any idea why this is happening?
 
You need to post the stack trace from your Logcat output
 
My logcat is

Java:
08-01 10:20:36.156 5739-5739/geepool.workonworkoff E/AndroidRuntime: FATAL EXCEPTION: main
                                                                     java.lang.NullPointerException
                                                                         at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:132)
                                                                         at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:65)
                                                                         at android.support.v7.app.AlertDialog.resolveDialogTheme(AlertDialog.java:111)
                                                                         at android.support.v7.app.AlertDialog.access$000(AlertDialog.java:70)
                                                                         at android.support.v7.app.AlertDialog$Builder.<init>(AlertDialog.java:291)
                                                                         at geepool.workonworkoff.Dialogs.WorkOnSavedDialog(Dialogs.java:25)
                                                                         at geepool.workonworkoff.WorkOn$1.onClick(WorkOn.java:99)
                                                                         at android.view.View.performClick(View.java:4084)
                                                                         at android.view.View$PerformClick.run(View.java:16966)
                                                                         at android.os.Handler.handleCallback(Handler.java:615)
                                                                         at android.os.Handler.dispatchMessage(Handler.java:92)
                                                                         at android.os.Looper.loop(Looper.java:137)
                                                                         at android.app.ActivityThread.main(ActivityThread.java:4745)
                                                                         at java.lang.reflect.Method.invokeNative(Native Method)
                                                                         at java.lang.reflect.Method.invoke(Method.java:511)
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
                                                                         at dalvik.system.NativeStart.main(Native Method)

thanks once again :)
 
Back
Top Bottom