0

The error is it showing in the run window. When I click the onClick Button This problem is occured and says an null object reference error. I have also attached the code here.

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.nochiketa.alarmapplication, PID: 4342
java.lang.NullPointerException: Attempt to invoke interface method 'void com.example.nochiketa.alarmapplication.MyDialog$onClickListener.onClick(java.lang.String, java.lang.String)' on a null object reference
    at com.example.nochiketa.alarmapplication.MyDialog.lambda$getAddClassDialog$3$MyDialog(MyDialog.java:93)

And My Mydialog.java program is here

public class MyDialog extends DialogFragment {
public static final String CLASS_ADD_DIALOG = "addClass";
public static final String STUDENT_ADD_DIALOG = "addStudent";

private onClickListener listener;
public interface onClickListener{
    void onClick(String text1, String text2);
}

public void setListener(onClickListener listener) {
    this.listener = listener;
}

private Dialog getAddClassDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    View view = LayoutInflater.from(getActivity()).inflate(R.layout.dialog, null);
    builder.setView(view);

    TextView title = view.findViewById(R.id.titleDialog);
    title.setText("Add New Class");

    EditText class_edt = view.findViewById(R.id.edt01);
    EditText subject_edt = view.findViewById(R.id.edt02);

    class_edt.setHint("Class Name");
    subject_edt.setHint("Subject Name");

    Button cancel = view.findViewById(R.id.cancel_btn);
    Button add = view.findViewById(R.id.add_btn);

    cancel.setOnClickListener(v -> dismiss());
    add.setOnClickListener(v -> {
        String className = class_edt.getText().toString();
        String subName = subject_edt.getText().toString();
        listener.onClick(className, subName);
        dismiss();
    });
return builder.create();
}

}

Can anyone tell me what is the problem?

Frank van Puffelen
  • 499,950
  • 69
  • 739
  • 734
  • 1
    `listener` is `null`. So either you never called `setListener`, or you called it with the value `null`. – Michael Aug 14 '21 at 07:12

0 Answers0