3
votes

Hello everyone I have a problem with FireBase create user and register user. Below is the code for register and login. I get nullPointer Exception and cant understand why .. the initialisation is made but cant figure it out so many days trying.

Register:

public class RegisterActivity extends Activity {

        private EditText mDisName;
        private EditText mMail;
        private EditText mPass;
        private Button reg;
        private TextView error;
        private FirebaseAuth regAuth;
        private ProgressBar myprog;
        private int Progress =0;
        private Handler myHandler= new Handler();
        private DatabaseReference databaseRef;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_register);
            mDisName =(EditText) findViewById(R.id.reg_name);
            mMail = (EditText) findViewById(R.id.reg_mail);
            mPass = (EditText) findViewById(R.id.reg_password);
            reg = (Button) findViewById(R.id.reg_button);
            error = (TextView) findViewById(R.id.textView4);
            myprog = (ProgressBar) findViewById(R.id.progressbar);
            regAuth = FirebaseAuth.getInstance();
            databaseRef = FirebaseDatabase.getInstance().getReference().child("UserNames :");

            reg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String disName = mDisName.getText().toString();
                    String dmail = mMail.getText().toString();
                    String pass = mPass.getText().toString();
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            while(Progress < 100) {
                                Progress++;
                                android.os.SystemClock.sleep(30);
                                myHandler.post(new Runnable() {
                                    @Override
                                    public void run() {
                                        myprog.setProgress(Progress);
                                    }
                                });
                            }

                        }
                    }).start();
                    registerUser(disName,dmail,pass);
                }
            });
        }



        public void registerUser(final String displayName,String email, String password){
            databaseRef.push().setValue(displayName);
         -->Error line <--   regAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) {
                            Intent loginIntent = new Intent(RegisterActivity.this, login.class);
                            startActivity(loginIntent);
                            Spannable errormes = new SpannableString("\"Registration Successful As User :\"" + displayName);
                            errormes.setSpan(new ForegroundColorSpan(Color.GREEN), 0, errormes.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                            error.setText(errormes);
                            //finish();
                        } else {
                            Toast.makeText(RegisterActivity.this, "Error with registration!", Toast.LENGTH_LONG).show();
                            error.setText("     Error 110! User already exists! \n    Please provide different username!");
                            try {
                                throw task.getException();
                            } catch (FirebaseAuthWeakPasswordException e) {
                                Log.e("Exception", e.getMessage());
                            } catch (FirebaseAuthInvalidCredentialsException e) {
                                Log.e("Exception", e.getMessage());
                            } catch (FirebaseAuthUserCollisionException e) {
                                Log.e("Exception", e.getMessage());
                            } catch (Exception e) {
                                Log.e("Exception", e.getMessage());
                            }
                        }
                    }
                });

        }

    }

Posting the log error for the register activity :

java.lang.NullPointerException at com.google.android.gms.internal.zzdvv.zzb(Unknown Source) at com.google.android.gms.internal.zzdwc.zza(Unknown Source) at com.google.firebase.auth.FirebaseAuth.createUserWithEmailAndPassword(Unknown Source) at com.package.myapp.RegisterActivity.registerUser(RegisterActivity.java:88) at com.package.myapp.RegisterActivity$1.onClick(RegisterActivity.java:79) at android.view.View.performClick(View.java:4438) at android.view.View$PerformClick.run(View.java:18422) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5017) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) at dalvik.system.NativeStart.main(Native Method)

Also same thing happens with login ( after registering user , using firebase plus button on site)

Login activity sample code :

java.lang.IllegalStateException: Could not execute method of the activity at android.view.View$1.onClick(View.java:3823) at android.view.View.performClick(View.java:4438) at android.view.View$PerformClick.run(View.java:18422) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5017) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at android.view.View$1.onClick(View.java:3818) at android.view.View.performClick(View.java:4438)  at android.view.View$PerformClick.run(View.java:18422)  at android.os.Handler.handleCallback(Handler.java:733)  at android.os.Handler.dispatchMessage(Handler.java:95)  at android.os.Looper.loop(Looper.java:136)  at android.app.ActivityThread.main(ActivityThread.java:5017)  at java.lang.reflect.Method.invokeNative(Native Method)  at java.lang.reflect.Method.invoke(Method.java:515)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)  at dalvik.system.NativeStart.main(Native Method)  Caused by: java.lang.NullPointerException at com.google.android.gms.internal.zzdvv.zzb(Unknown Source) at com.google.android.gms.internal.zzdwc.zzb(Unknown Source) at com.google.firebase.auth.FirebaseAuth.signInWithEmailAndPassword(Unknown Source) at com.package.myapp.login.login(login.java:143)

Also providing the login java class below :

public class login extends Activity {

    private EditText user;
    private EditText password;
    private TextView registerView;
    private TextView loginView;
    private Spannable errormes;
    private FirebaseAuth mAuth;
    private Button login;
    private CallbackManager callbackManager ;
    private ImageView loginIma;
    private ImageView image;
    private ImageView image2;
    private CardView cardView;

    private GoogleSignInOptions gso;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        user = (EditText) findViewById(R.id.username);
        image=(ImageView)findViewById(R.id.imageView4);
        image2=(ImageView)findViewById(R.id.imageView3);
        errormes = new SpannableString("Error 100! User doesnt exists! \n Please register first");
        userdatabase = FirebaseDatabase.getInstance().getReference().child("Users :");
        mAuth = FirebaseAuth.getInstance();
        registerView = (TextView) findViewById(R.id.registerView);
        password = (EditText)findViewById(R.id.passwordView);
        callbackManager = CallbackManager.Factory.create();
        boolean loggedIn = AccessToken.getCurrentAccessToken() == null;
        cardView=(CardView)findViewById(R.id.cardView);
        loginView = (TextView) findViewById(R.id.loginView);

        callbackManager = CallbackManager.Factory.create();

        LoginManager.getInstance().registerCallback(callbackManager,
                new FacebookCallback<LoginResult>() {
                    @Override
                    public void onSuccess(LoginResult loginResult) {
                        Intent account = new Intent(login.this,menu_activity.class);
                        startActivity(account);
                    }

                    @Override
                    public void onCancel() {
                        Intent main = new Intent(login.this,login.class);
                        startActivity(main);
                        finish();

                    }

                    @Override
                    public void onError(FacebookException exception) {
                        Toast.makeText(login.this, "Error 500! Facebook login failed!", Toast.LENGTH_SHORT).show();
                    }
                });

    }





    private int size = 0;
    private HashMap<String, String> users = new HashMap<String, String>();

    private DatabaseReference userdatabase;

    private FirebaseAuth.AuthStateListener mAuthListener;


    public void setSize(int newsize) {
        this.size = newsize;
    }

    public void login(View view) {
        EditText user = (EditText) findViewById(R.id.username);
        EditText password = (EditText) findViewById(R.id.passwordView);
        Spannable errormes = new SpannableString("Error 100! User doesnt exists! \n Please register first");
        errormes.setSpan(new ForegroundColorSpan(Color.RED), 0, errormes.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        Intent userAcc = new Intent(login.this,menu_activity.class);
        startActivity(userAcc);

    **//error line** mAuth.signInWithEmailAndPassword(user.getText().toString(), password.getText().toString())
                        .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                            @Override
                            public void onComplete(@NonNull Task<AuthResult> task) {
                                if (task.isSuccessful()) {
                                    // Sign in success, update UI with the signed-in user's information
                                    Log.d("Loged on!", "signInWithEmail:success");
                                    Toast.makeText(login.this, "Loged On!.", Toast.LENGTH_SHORT).show();
                                    Intent userAcc = new Intent(login.this,menu_activity.class);
                                    startActivity(userAcc);
                                } else {
                                    // If sign in fails, display a message to the user.
                                    Log.w("Login Failed!", "signInWithEmail:failure", task.getException());
                                    Toast.makeText(login.this, "Authentication failed.", Toast.LENGTH_SHORT).show();
                                }

                                // ...
                            }
                        });
        }

If anyone can help would be glad. Thanks in advance!

1
have you checked the parameters you are passing to registration and login?Rahul Chandrabhan
Look in the logcat at the time the app initializes for this warning: W/GooglePlayServicesUtil: Google Play services out of date. The device or emulator must have a version of Play services installed that supports the version of the Firebase SDK the app is built with.Bob Snyder
Benjith binja thank you for editing still new ! @BobSnyder will look at thisJohn Luko
@BobSnyder yeap worked thanks you very muchJohn Luko

1 Answers

0
votes

Thanks to @BobSnyder , the problem was on the api. You have to install API with (Google APIs) add on.