0

I am creating an AlertDialog that I can enter a new value into a SQLite database. The Insert method works if I call it by itself in the onCreate() method, but when I try to call the method in the setPositiveButton onclick listener it doesn't work, it gives me a java.lang.NullPointerException error. I have tried to save the value to a variable and pass it into the method and it still gives me the same error where I try to assign the value. I have even created new buttons and tried to call an onclick on them and it still gives me the same error. Does anyone know what I am doing wrong, thanks.

public void onAddItemClick(View view){
    // get the add_item view
    LayoutInflater layoutInflater = LayoutInflater.from(context);
    View addView = layoutInflater.inflate(R.layout.add_item, null);

    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);

    // Set add_item to alertdialog builder
    alertDialogBuilder.setView(addView);

    final EditText insertValue = (EditText)findViewById(R.id.txtInsert);

    // Set Dialog Message
    alertDialogBuilder.setCancelable(false).setPositiveButton("Add to Database",
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int id) {
                    // Will Create a new course and insert into the database
                    insertNewCourse(insertValue.getText().toString());
                }
            })
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int id) {
                    dialogInterface.cancel();
                }
            });

    // Create alert dialog
    AlertDialog alertDialog = alertDialogBuilder.create();

    // show it now
    alertDialog.show();
}

And here is the logcat message:

java.lang.NullPointerException
            at com.example.gradetracker.app.MainActivity$2.onClick(MainActivity.java:77)
            at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)
2
Contributors
2
Replies
16
Views
3 Years
Discussion Span
Last Post by monkeybut
1

If i read it correctly your insertValue R.id.txtInsert is part of R.layout.add_item. If that is case then

final EditText insertValue = (EditText)findViewById(R.id.txtInsert);

will be null because edit text was not found. You have to find it in coresponding view, so it should be like

final EditText insertValue = (EditText) addView.findViewById(R.id.txtInsert);
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.