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
NullPointerException
s 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
Android - Activity VS Fragmentactivity
How to View the Shared Preferences File Using Android Studio
Add Floating Point Value to Android Resources/Values
Fragment in Viewpager Using Fragmentpageradapter Is Blank the Second Time It Is Viewed
How to Add Button Tint Programmatically
Is Using Serializable in Android Bad
Catch on Swipe to Dismiss Event
Android - Keep Listview's Item Highlighted Once One Has Been Clicked
Bluetooth and Wifi Printing for Android
Android Character by Character Display Text Animation
Differencebetween Background, Backgroundtint, Backgroundtintmode Attributes in Android Layout Xml
How to Use Mapview in Android Using Google Map V2
Android Device Chooser - My Device Seems Offline
How to Start an Intent by Passing Some Parameters to It