0

Causing error when setText a value in edittext. Id Given is Correct ,if i give set Attempt to invoke virtual method 'void android.widget.EditText.setText(java.lang.CharSequence)' on a null object reference

Code Given Below here i cant setText Otp Code "otpcode.setText("12345");" in oncreate it works perfectely. when i give it in the method"recivedSms".it didn't work.

public class Change_Password_Activity extends AppCompatActivity {
EditText user_name,pass_wd;
public   EditText otpcode;
private Button btn_submit;
private String username,otp,password;
private ProgressDialog prgDialog;
private Typeface typeface;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_change__password_);
    typeface = GlobalVariables.getTypeface(Change_Password_Activity.this);
    prgDialog = new ProgressDialog(this);
    // Set Progress Dialog Text
    prgDialog.setMessage("Please wait...");
    // Set Cancelable as False
    prgDialog.setCancelable(false);

    otpcode = (EditText)findViewById(R.id.otpedittext);
    user_name = (EditText) findViewById(R.id.edittext_ch_user);
    pass_wd = (EditText) findViewById(R.id.edittext_ch_passwd);
    btn_submit = (Button) findViewById(R.id.button_changepswd);
    otpcode.setTypeface(typeface);
    user_name.setTypeface(typeface);
    pass_wd.setTypeface(typeface);
    btn_submit.setTypeface(typeface);



}
public void recivedSms(String message)
{
    try
    {
        int smsnumbr= Integer.parseInt(message);
        otpcode.setText(smsnumbr);

    }
    catch (Exception e)
    {
        Log.e("error", String.valueOf(e));
    }
susaine
  • 141
  • 1
  • 9

1 Answers1

0

if you are trying to set smsnumbr to edittext then it will give null pointer exception as in setText(Integer) android tries to find a resource from R.java file with given integer as id. to achieve what you want you should use String.valueOf(..) instead.

int smsnumbr= Integer.parseInt(message);
otpcode.setText(String.valueOf(smsnumb));
Aditya Chauhan
  • 271
  • 3
  • 13