0

i am registering users to my app with this code,my app crashes on registration and gives a

"FATAL EXCEPTION: main
java.lang.NullPointerException
at com.example.ghost.prochat.MainActivity$1.onComplete(MainActivity.java:134)
at com.google.android.gms.tasks.zzc$1.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5069)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)"

My code has no syntax errors in that line, what can I do?

Here the method that register the user with Firebase:

FirebaseAuth.getInstance().createUserWithEmailAndPassword(email, password) .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
        @Override
        public void onComplete(@NonNull Task<AuthResult> task) {
            Logger.getLogger(LoginActivity.class.getName()).log(Level.ALL, "createUserWithEmailAndPassword:onComplete:" + task.isSuccessful());
            registerProgressDlg.dismiss();
            if (!task.isSuccessful()) {
                Logger.getLogger(MainActivity.class.getName()).log(Level.ALL, "createUserWithEmailAndPassword", task.getException());
                Utils.showDialog(
                        MainActivity.this,
                        getString(R.string.err_singup));
            }
            else {
                final ArrayList<String> defaultRoom = new ArrayList<String>();
                defaultRoom.add("home");
                // Update the user profile information
                final FirebaseUser user = task.getResult().getUser();
                UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder()
                        .setDisplayName(String.valueOf(displayUsername))
                        .setPhotoUri(Uri.parse("https://example.com/jane-q-user/profile.jpg"))
                        .build();
                user.updateProfile(profileUpdates).addOnCompleteListener(new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {
                        if (task.isSuccessful()) {
                            Logger.getLogger(MainActivity.class.getName()).log(Level.ALL, "User profile updated.");
                            // Construct the ChatUser
                            UserList.user = new ChatUser(user.getUid(),displayUsername, editTextEmail,true,defaultRoom);
                            // Setup link to users database
                            FirebaseDatabase.getInstance().getReference("users").child(user.getUid()).setValue(UserList.user);
                            startActivity(new Intent(MainActivity.this, UserList.class));
                            finish();
                        }
                    }
                });
            }
        }
    });
}
MikeKeepsOnShine
  • 1,730
  • 4
  • 23
  • 35
Sir George
  • 423
  • 3
  • 8
  • 20
  • What line are you getting your NullPointerException? I.e. which line is 134 in the code you pasted? – barq Dec 23 '16 at 08:58
  • 3
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – barq Dec 23 '16 at 08:59
  • please share some more code of error log and your activity code.I think you haven't initialized instance of FirebaseAuth . – Farmer Dec 23 '16 at 09:03
  • Post the line 134 of your Activity. The log is clear, the error is there – MikeKeepsOnShine Dec 23 '16 at 09:06
  • @barq registerProgressDlg.dismiss(); is my line 134 – Sir George Dec 23 '16 at 09:06
  • Based on your comment, the `registerProgressDIg` is not initialized. – MikeKeepsOnShine Dec 23 '16 at 09:09
  • post complete stack trace of your crash. – Gopal Dec 23 '16 at 09:10
  • @MikeKeepsOnShine said your `registerProgressDlg.dismiss()` please comment this line and run again. – Farmer Dec 23 '16 at 09:14
  • @SirGeorge Please show your code for your declaration/initialization of registerProgressDlg. – barq Dec 23 '16 at 09:14
  • i initialised the registerProgressDlg, but now i get Process: com.example.ghost.prochat, PID: 4453 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ProgressDialog.dismiss()' on a null object reference at com.example.ghost.prochat.MainActivity$1.onComplete(MainActivity.java:137) – Sir George Dec 23 '16 at 09:32
  • @MikeKeepsOnShine when i comment the line registerProgressDlg.dismiss() i get an Error message by the app" Error occured while signing up" – Sir George Dec 23 '16 at 09:37
  • @SirGeorge i never said to comment the line. I said that the error is in that line and to check the initialization, check my answer – MikeKeepsOnShine Dec 23 '16 at 09:41

2 Answers2

0

Based on your comment, the registerProgressDIg is the problem.

It is not initialized, try to do this, meanwhile:

if(registerProgressDIg!= null && registerProgressDIg.isShowing()){
       registerProgressDIg.dismiss();
}

BTW, check where you instantiate and initialize the registerProgressDIg

MikeKeepsOnShine
  • 1,730
  • 4
  • 23
  • 35
  • now the app doesnt crash but i am getting an error "Error registering user" – Sir George Dec 23 '16 at 11:35
  • That error is given by Firebase, so it is another scenario :) – MikeKeepsOnShine Dec 23 '16 at 11:39
  • can you look at this serviceID=16, operation=ValidateAuthServiceOperation java.lang.NullPointerException: onPostInitComplete can be called only once per call to getRemoteService at kqa.a(:com.google.android.gms:74) at kna.a(:com.google.android.gms:987) at knw.a(:com.google.android.gms:66) – Sir George Dec 23 '16 at 12:33
  • That should be caused beacuse you nested two calls. Try to create a separate method that does the 'user.updateProfile' operations. e.g: you add a listener on irebaseAuth.getInstance().createUserWithEmailAndPassword, get the result and AFTER THAT, based on the result, you call (or not) user.updateProfile – MikeKeepsOnShine Dec 23 '16 at 12:59
0

As @MikeKeepsOnShine i think your ProgressDialog is Null. You have not initialized and try to dismiss() in you onComplete() method. First you have to initialized and that after dismiss. Please try this.

ProgressDialog registerProgressDIg = new ProgressDialog(MainActivity.this)
registerProgressDIg.show()

FirebaseAuth.getInstance().createUserWithEmailAndPassword(email, password) .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
        @Override
        public void onComplete(@NonNull Task<AuthResult> task) {


            if(registerProgressDIg!= null && registerProgressDIg.isShowing()){
                registerProgressDIg.dismiss();   
            }

            //paste your code as it.

    });
}
Farmer
  • 4,093
  • 3
  • 23
  • 47