skip to Main Content

I am creating an OTP verification app using Firebase Authentication with a phone number. I did all the stuff like add dependencies,pugin, JSON file, and connect Firebase Authentication but still have this error. please tell me what should i do.

public class MainActivity extends AppCompatActivity {

    EditText editMobileNumber;
    Button sendButton;
    FirebaseAuth mAuth;
    PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mAuth=FirebaseAuth.getInstance();

        editMobileNumber=findViewById(R.id.editMobileNumber);
        sendButton=findViewById(R.id.getOTP);

        sendButton.setOnClickListener(v -> {
            String number=editMobileNumber.getText().toString().trim();
            if(number.length() != 10){

                Toast.makeText(this,"Enter a valid number",Toast.LENGTH_SHORT).show();
            }
            else{
                    otpSend(number);
            }
        }
        );

    }

    private void otpSend(String number) {
        mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {

            @Override
            public void onVerificationCompleted(@NonNull PhoneAuthCredential credential) {

            }

            @Override
            public void onVerificationFailed(@NonNull FirebaseException e) {

                Toast.makeText(MainActivity.this,number,Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCodeSent(@NonNull String verificationId,
                                   @NonNull PhoneAuthProvider.ForceResendingToken token) {

                Toast.makeText(MainActivity.this,verificationId,Toast.LENGTH_SHORT).show();
                Intent i=new Intent(MainActivity.this, verifyOTP.class);
                i.putExtra("verificationId",verificationId);
                startActivity(i);
                finish();
            }
        };

        PhoneAuthOptions options =
                PhoneAuthOptions.newBuilder(mAuth)
                        .setPhoneNumber("+91" + number)       // Phone number for Otp
                        .setTimeout(60L, TimeUnit.SECONDS) // Timeout and unit
                        .setActivity(this)
                        .setCallbacks(mCallbacks)          // OnVerificationStateChangedCallbacks
                        .build();

        PhoneAuthProvider.verifyPhoneNumber(options);

    }


}

verification is on next activity. here is the code :-

public class verifyOTP extends AppCompatActivity {

PinView otpPinView;
Button resendOTPButton,verifyButton;
TextView textView;
String verificationId= getIntent().getStringExtra("verificationId"),otp;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_verify_otp);

    otpPinView=findViewById(R.id.otppinview);
    resendOTPButton=findViewById(R.id.resendOTPButton);
    verifyButton=findViewById(R.id.verifyButton);
    textView=findViewById(R.id.verifyOTP);



    textView.setOnClickListener(v -> {
        Toast.makeText(this,"it works",Toast.LENGTH_SHORT).show();
    });


    verifyButton.setOnClickListener(v ->{
        otp= Objects.requireNonNull(otpPinView.getText()).toString();

        if(otp.length() != 6){
            Toast.makeText(this,"Invalid OTP!!",Toast.LENGTH_SHORT).show();
        }
        else{
            if(verificationId != null){
                PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationId, otp);

                FirebaseAuth.getInstance().signInWithCredential(credential)
                        .addOnCompleteListener(this, task -> {
                            if (task.isSuccessful()) {
                                // If Sign in success


                            } else {
                                // If sign in failed
                                Toast.makeText(this,"Invalid OTP!!",Toast.LENGTH_SHORT).show();
                            }
                        });

            }
        }

    });


}

}

the logcat show error is:-

  1. Failed to initialize reCAPTCHA config: No Recaptcha Enterprise siteKey configured for tenant/project *

  2. FATAL EXCEPTION: main java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.vivetales/com.example.vivetales.verifyOTP}: java.lang.NullPointerException: Attempt to invoke virtual method ‘java.lang.String android.content.Intent.getStringExtra(java.lang.String)’ on a null object reference

2

Answers


  1. Check if phone authentication is enabled in the Firebase console

    (!FirebaseAuth.getInstance().getSignInMethods().contains(FirebaseAuth.PHONE_SIGN_IN_METHOD)) {
            Toast.makeText(this, "Phone authentication is disabled for this project", Toast.LENGTH_SHORT).show();
            return;
        }
    
    Login or Signup to reply.
  2. You’re getting this error:

    FATAL EXCEPTION: main java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.vivetales/com.example.vivetales.verifyOTP}: java.lang.NullPointerException: Attempt to invoke virtual method ‘java.lang.String android.content.Intent.getStringExtra(java.lang.String)’ on a null object reference

    Because of the following line of code:

    String verificationId= getIntent().getStringExtra("verificationId"),otp;
    

    By the time you’re calling getIntent()., there is no intent available at that point. This means that getIntent() is null. So you cannot call getIntent() before onCreate() is called. So to solve this, you have to move the declaration of verificationId inside onCreate().

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search