1

I have got this error


Attempt to invoke virtual method 'void android.widget.EditText.setText(java.lang.CharSequence, android.widget.TextView$BufferType)' on a null object reference


while I was trying to set some data into EditText using setText method but it points to null and makes the app down, the photo below shows that the edit texts are null even though I set the data:


enter image description here


please advise me. Thank you!

update my code :


enter code here : public class AddUserDialog extends DialogFragment {


public interface OnInputUserTypeListener {

    void sendInputAdd(UserInfoModel userInfoModel);

    void sendInputUpdate(int position, UserInfoModel userInfo);

}

public OnInputUserTypeListener onInputUserTypeListener;

private int PICK_IMAGE_REQUEST = 1;
public Button add;
public EditText editTextName, editTextEmail, editTextPhone;
public CircleImageView imageViewaddPhoto;
public TextView cancel;
public Bitmap bitmap;
String emailPattern = "[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+";
UserInfoModel userInfo;
int position=-1;
String type;
Uri uri;


public AddUserDialog(String type) {
    this.type = type;

}

public AddUserDialog(int position,UserInfoModel userInfo ,String type) {
    this.userInfo = userInfo;
    this.type = type;
    this.position = position;


    loadUserData();

}

public AddUserDialog(int addposition,String addtype) {

    this.type = addtype;
    this.position = addposition;




}

public void loadUserData() {
    String phone = userInfo.getPhone();
    String name = userInfo.getName();
    String email = userInfo.getEmail();
   // Bitmap photo = userInfo.getPhoto();

    editTextName.setText(name,TextView.BufferType.EDITABLE);
    editTextPhone.setText(phone,TextView.BufferType.EDITABLE);
    editTextEmail.setText(email,TextView.BufferType.EDITABLE);
   // imageViewaddPhoto.setImageBitmap(photo);

}

@NonNull
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.add_user_dialog, container, false);
    initialize(v);

    listeners();


    return v;

}

public void initialize(View v) {
    add = v.findViewById(R.id.button);
    cancel = v.findViewById(R.id.cancel_dialog);
    editTextName = v.findViewById(R.id.name);
    editTextEmail = v.findViewById(R.id.email);
    editTextPhone = v.findViewById(R.id.phone);
    imageViewaddPhoto = v.findViewById(R.id.imageView);

}

public void listeners() {

    cancel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            getDialog().dismiss();
        }
    });


    imageViewaddPhoto.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            chooseImage();
        }
    });

    add.setOnClickListener(new View.OnClickListener() {
        @RequiresApi(api = Build.VERSION_CODES.KITKAT)
        @Override
        public void onClick(View v) {


            if (TextUtils.isEmpty(editTextName.getText())) {

                editTextName.setError(getResources().getString(R.string.required));
                editTextName.requestFocus();

                return;
            }

            if (TextUtils.isEmpty(editTextPhone.getText())) {

                editTextPhone.setError(getResources().getString(R.string.required));
                editTextPhone.requestFocus();

                return;
            }

            if (TextUtils.isEmpty(editTextEmail.getText())) {

                editTextEmail.setError(getResources().getString(R.string.required));
                editTextEmail.requestFocus();

                return;
            }


            if (editTextEmail.getText().toString().trim().matches(emailPattern)) {

                editTextEmail.requestFocus();
            } else {
                editTextEmail.setError(getResources().getString(R.string.invalid));
                editTextEmail.requestFocus();
                return;
            }


            String name = editTextName.getText().toString();
            String email = editTextEmail.getText().toString();
            String phone = editTextPhone.getText().toString();

            imageViewaddPhoto.setDrawingCacheEnabled(true);


            if (!name.isEmpty() || (!phone.isEmpty() || !email.isEmpty())) {
                if (type.equals("new")) {
                    UserInfoModel u1 = new UserInfoModel(name, email, phone, bitmap);
                    onInputUserTypeListener.sendInputAdd(u1);

                    Snackbar.make(v, "User added", Snackbar.LENGTH_LONG)
                            .setAction("Action", null).show();

                    Objects.requireNonNull(getDialog()).dismiss();
                }


                if (type.equals("update")) {

                    UserInfoModel u1 = new UserInfoModel(name, email, phone, bitmap);
                    onInputUserTypeListener.sendInputUpdate(position, u1);

                    Snackbar.make(v, "User updated", Snackbar.LENGTH_LONG)
                            .setAction("Action", null).show();

                    Objects.requireNonNull(getDialog()).dismiss();

                }
            }

        }
    });


}


public void chooseImage() {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);

}

@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {

        uri   = data.getData();

        try {
            bitmap = MediaStore.Images.Media.getBitmap(getActivity().getApplicationContext().getContentResolver(), uri);


            imageViewaddPhoto.setImageBitmap(bitmap);


        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}


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

    try {
        onInputUserTypeListener = (OnInputUserTypeListener) getActivity();
    } catch (ClassCastException e) {

    }
}

}

1 Answers1

1

You are calling setText() methods on EditTexts inside loadUserData() method which is located inside AddUserDialog class constructor. But findViewById() methods are located inside onCreateView() callback method inside initialize() method.

Since constructor method is called before onCreateView() callback method, loadUserData() method is called before initialize() which means setText() method is called before EditTexts bind with UI through findViewById() methods.

You can try replacing editTextName.setText() methods inside onCreateView() callback or in a seperate onCreate() callback. So EditTexts bind with UI first then setText() methods are called on EditText objects.

barutto
  • 104
  • 1
  • 14