0

The line 'listener2.applyTexts2(infor);' gives me an error that I pass a null object reference on applyTexts2. I debugged this line and 'infor' has all values passed to it normally, yet it still says that it is null. What did I do wrong?

(I am calling this from my MainActivity from a button and the popup to insert the info is fine, everything looks good, yet it says about null when I have no null in the data. I have another listener in my MainActivity and it works fine. Does the first one somehow mess the second one? They are completely separate classes)

public class StaffDialog extends AppCompatDialogFragment{

    private StaffDialogListener listener2;
    private EditText FirstName;
    private EditText LastName;
    private EditText Phone;
    private EditText Linkedin;
    private EditText Tran;
    private String[] infor = new String[5];

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        LayoutInflater inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.layout_dialog2,null);

        builder.setView(view)
                .setTitle("Enter this person's information")
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                })
                .setPositiveButton("Done", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        infor[0]=FirstName.getText().toString();
                        infor[1]=LastName.getText().toString();
                        infor[2]=Phone.getText().toString();
                        infor[3]=Linkedin.getText().toString();
                        infor[4]=Tran.getText().toString();
                        listener2.applyTexts2(infor);
                    }
                });

        FirstName = view.findViewById(R.id.name);
        LastName = view.findViewById(R.id.lastname);
        Phone = view.findViewById(R.id.phone);
        Linkedin = view.findViewById(R.id.linkedin);
        Tran = view.findViewById(R.id.tran);
        return builder.create();
    }

    @Override
    public void onAttach(Context context){
        super.onAttach(context);

        try{
            listener2 = (StaffDialogListener) context;
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    public interface StaffDialogListener{
        void applyTexts2(String[] info);
    }
}```
Android_id
  • 1,521
  • 1
  • 15
  • 33
Dan
  • 49
  • 6
  • 1
    I'm I missing something here, you have an interface StaffDialogListener that I dont see where is implemented. And you set to listener2 just context casted to StaffDialogListener. – soynerdito May 21 '20 at 19:29
  • You're right. That was the problem. I didn't implement StaffDialogListener in my MainActivity. Thank you very much! – Dan May 22 '20 at 20:06

0 Answers0