1

I'm having difficulty understanding something with my app.

I am using Firebase to provide user authentication which works fine until I try it on other phones/devices.

When I put in the email and password and press 'Login', the app crashes and the log gives the following:

FATAL EXCEPTION: main Process: com.example.android.frapp, PID: 14822
                                                                       java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.tasks.Task com.google.android.gms.common.api.GoogleApi.zzb(com.google.android.gms.common.api.internal.zzdf)' on a null object reference
                                                                           at com.google.android.gms.internal.zzdtp.zzb(Unknown Source)
                                                                           at com.google.android.gms.internal.zzdtw.zzb(Unknown Source)
                                                                           at com.google.firebase.auth.FirebaseAuth.signInWithEmailAndPassword(Unknown Source)
                                                                           at com.example.android.frapp.MainLoginActivity.startSiginIn(MainLoginActivity.java:105)
                                                                           at com.example.android.frapp.MainLoginActivity.access$000(MainLoginActivity.java:23)
                                                                           at com.example.android.frapp.MainLoginActivity$2.onClick(MainLoginActivity.java:65)
                                                                           at android.view.View.performClick(View.java:5197)
                                                                           at android.view.View$PerformClick.run(View.java:20926)
                                                                           at android.os.Handler.handleCallback(Handler.java:739)
                                                                           at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                           at android.os.Looper.loop(Looper.java:145)
                                                                           at android.app.ActivityThread.main(ActivityThread.java:5951)
                                                                           at java.lang.reflect.Method.invoke(Native Method)
                                                                           at java.lang.reflect.Method.invoke(Method.java:372)
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)

It's all happening from my main login class (which I have provided below):

public class MainLoginActivity extends AppCompatActivity {

private EditText mEmailField;
private EditText mPasswordField;

private Button mLoginBtn;

private FirebaseAuth mAuth;

private FirebaseAuth.AuthStateListener mAuthListener;

private boolean isUserClickedBackButton = false;

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

    mAuth = FirebaseAuth.getInstance();

    mEmailField = (EditText) findViewById(R.id.emailField);
    mPasswordField = (EditText) findViewById(R.id.passwordField);

    mLoginBtn = (Button) findViewById(R.id.loginBtn);

    // connection to user authentication on firebase (database)
    mAuthListener = new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {

            if (firebaseAuth.getCurrentUser() != null){

                startActivity(new Intent(MainLoginActivity.this, MainActivity.class));
            }

        }
    };

    mLoginBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            startSiginIn();


        }
    });
}

@Override
protected void onStart() {
    super.onStart();

    mAuth.addAuthStateListener(mAuthListener);
}

@Override
// code for exiting from app by using back button on login page
public void onBackPressed() {
    //moveTaskToBack(true);
    if (!isUserClickedBackButton){
        Toast.makeText(this, "Press back again to exit", Toast.LENGTH_SHORT).show();
        isUserClickedBackButton = true;
    } else {
        System.exit(0); // exits right out of app
        super.onBackPressed();

    }
}


// for login
private void startSiginIn() {

    String email = mEmailField.getText().toString();
    String password = mPasswordField.getText().toString();

    if (TextUtils.isEmpty(email) || TextUtils.isEmpty(password)) {

        Toast.makeText(MainLoginActivity.this, "Please input email and password", Toast.LENGTH_LONG).show();

    } else {
        mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {

                if (!task.isSuccessful())

                    Toast.makeText(MainLoginActivity.this, "Sign in problem.  Please check email" +
                            " and password", Toast.LENGTH_LONG).show();
            }
        });
    }
}
}

As I say, it works perfect on my main device but doesn't when I install and try to login on other devices. And it also works fine on the emulator.

I've tried searching for similar problems but can't find any.

Has anyone got any ideas?

Thanks

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
PAdams
  • 67
  • 11
  • 1
    Why am I being down marked down without explanation? I'm trying my best here – PAdams Feb 15 '18 at 22:04
  • You're doing fine. Keep the faith! There are some here on SO who get pleasure from being mean-spirited. We all were new developers at some point. I get it! – Native_Mobile_Arch_Dev Feb 16 '18 at 02:11
  • @Kling Klang - I think you'll find my question wasn't an exact duplicate. The error may have been the same but the question surrounding the error most certainly was not. Alex Mamo got it and provided me with the correct answer – PAdams Feb 16 '18 at 13:28
  • You will find that **every NPE is the same story**. An object has been used before it has been referenced. – Phantômaxx Feb 16 '18 at 13:45

1 Answers1

1

If your authentication process works fine on your phone/emulator but you get that error when testing on another device it means that most likely that device does not have Google Play Services installed. Make sure it is installed correctly and try again.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • That's the solution. I had Google Play Services but it wasn't up-to-date. Thank you. I've marked as the correct answer – PAdams Feb 16 '18 at 13:26
  • Just up-voted it for you. Wouldn't let me do it initially but now it has – PAdams Feb 16 '18 at 13:29