0

Here is the error that I have been getting: java.lang.NullPointerException: Attempt to invoke virtual method 'androidx.fragment.app.Fragment androidx.fragment.app.FragmentManager.findFragmentById(int)' on a null object reference at com.skyloromnisecurity.skylor.common.ContactEditTexts.findEditText(ContactEditTexts.java:44) Line 44 is 'Fragment fragment = fragmentManager.findFragmentById(fragmentId);'

`public class ContactEditTexts { private List contacts = new ArrayList<>(); private Context context;

public ContactEditTexts(FragmentManager fragmentManager, final Button bActionButton,
                        Context context) {
    this.context = context;

    TextWatcher phoneNumberTextWatcher = phoneNumberWatcher(bActionButton);
    List<Integer> ids = Arrays.asList(first_contact, second_contact, third_contact);
    for (Integer id : ids) {
        EditText editText = findEditText(id, fragmentManager);
        editText.addTextChangedListener(phoneNumberTextWatcher);
        contacts.add(editText);
    }
}

private EditText findEditText(int fragmentId, FragmentManager fragmentManager) {
Fragment fragment = fragmentManager.findFragmentById(fragmentId);
    assert fragment != null;
    return (EditText) fragment.requireView().findViewById(R.id.contact_edit_text);
}`
Eugene Hoyt
  • 33
  • 1
  • 3
  • Change signature to `@NonNull FragmentManager fragmentManager` to get another error message. – Martin Zeitler May 22 '20 at 16:33
  • Also just a friendly suggestion when it says too much code it doesn't mean you convert some of the code to text. Sure post the code, but just explain it as well. – Aditya Kurkure May 22 '20 at 16:34
  • I really appreciate the feedback. I changed the code and added the "@NonNull" but still have the same error. – Eugene Hoyt May 22 '20 at 16:50
  • How about stopping to pass `null` into the method? Generally speaking, the code provided is merely irrelevant, because it is not clear how the method is being called. – Martin Zeitler May 22 '20 at 16:54
  • Thank you you for your assistance in advance. Could you provide me an example? I tried passing a "null" a couple different ways with the same result. – Eugene Hoyt May 22 '20 at 17:09

0 Answers0