Sms Authentication on Android Nullpointerexception

Login to Firebase using phone returns null pointer

Ok so the way i solved this problem was to move the mcallbacks to the on create section of code. as shown below

setContentView(R.layout.activity_phone__login);
mAuth = FirebaseAuth.getInstance();
InitVariables();
AddPhoneNumberButtons();

mcallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
@Override
public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {
signInWithPhoneAuthCredential(phoneAuthCredential);
}

@Override
public void onVerificationFailed(FirebaseException e) {

Toast.makeText(getBaseContext(), "Wrong Or Invalid Phone Number...", Toast.LENGTH_SHORT).show();
AddPhoneNumberButtons();
if (e instanceof FirebaseAuthInvalidCredentialsException) {
Toast.makeText(getBaseContext(), "Invalid Request " + e.toString(), Toast.LENGTH_SHORT).show();
} else if (e instanceof FirebaseTooManyRequestsException) {
Toast.makeText(getBaseContext(), "The SMS quota for the project has been exceeded " + e.toString(), Toast.LENGTH_SHORT).show();
}

}
@Override
public void onCodeSent(String verificationId,
PhoneAuthProvider.ForceResendingToken token) {
// Save verification ID and resending token so we can use them later
verificationid = verificationId;
mresendtoken = token;
Toast.makeText(getBaseContext(), "Code Sent Please Check Your SMS...", Toast.LENGTH_SHORT).show();
AddVerifyButtons();

}
};

Firebase Auth causing Fatal exeception: (java.lang.NullPointerException)

You never initialize auth, which means that by the time you execute auth!!.signInWithEmailAndPassword(email, password) you get an exception.

Initialize auth like shown in step 2 here in your onCreate to prevent the error.

// Initialize Firebase Auth
auth = Firebase.auth

NullPointerExceptions are very common, so I highly recommend learning how to troubleshoot these yourself. For that, check out:

  • What is the Kotlin double-bang (!!) operator?
  • Unfortunately MyApp has stopped. How can I solve this?
  • What is a NullPointerException, and how do I fix it?

Firebase SMS Verification / Authentication not working on some devices

If you have connected firebase with the help of android assistant these kind of issues may show up.So you need to download a new copy of google-services.json file from firbase console and add it to your project folder. This might help you.



Related Topics



Leave a reply



Submit