Home Android firebase register , current user null pointer exception when trying to get display name
Reply: 3

Android firebase register , current user null pointer exception when trying to get display name

John Luko
1#
John Luko Published in 2018-01-13 00:15:32Z

I have the problem below , I have this sample code : And when im trying to use this one --> "databaseRef.child(user.getDisplayName()).setValue(displayName);" I get the error below. Anyone can explain me why this is happening? Seems pretty normal to me cant understand why the error is generated.

public void registerUser(final String displayName, String email, final String password){
        regAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {
                        FirebaseUser user=regAuth.getCurrentUser();
                        UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder()
                                .setDisplayName(displayName).build();
                        user.updateProfile(profileUpdates);
                        databaseRef.push().child(user.getDisplayName()).setValue(displayName);
                        FirebaseAuth.getInstance().signOut();
                        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);
                        Intent loginIntent = new Intent(RegisterActivity.this, login.class);
                        startActivity(loginIntent);
                        finish();
                    } else {
                        if(password.length()<6)
                            error.setText("Password too Short! Must be at least 6 characters long! \n Please provide different password.");
                        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());
                        }
                    }
                }
            });

    }

java.lang.NullPointerException: Can't pass null for argument 'pathString' in child() at com.google.firebase.database.DatabaseReference.child(Unknown Source) at com.myapp.RegisterActivity$2.onComplete(RegisterActivity.java:97) at com.google.android.gms.tasks.zzf.run(Unknown Source) 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)

Prateek
2#
Prateek Reply to 2018-01-13 00:56:06Z

Check the value of your databaseRef value and where its pointing to. Make sure data base reference does exist for databaseRef.child. Looks like user.getDisplayName() is returning NULL hence its mapped to

databaseRef.child(NULL).setValue(displayName); 

Try to print all the values in console or log and cross check with Firebase database on browser.

Also try updates with listener

user.updateProfile(profileUpdates).
    addOnCompleteListener(new OnCompleteListener<Void>() {
                                                        @Override
                                                        public void onComplete(@NonNull Task<Void> task) {
                                                            Log.d("Success", "Success");
                                                        }
                                                        });
cricket_007
3#
cricket_007 Reply to 2018-01-13 00:58:49Z

The fact that you are setting the name, the trying to get the value using a method is concerning. You already have the value, so using a getter isn't needed.

Your user reference probably shouldn't be using a display name that's the key of the database, however. Especially if multiple users can have the same name. You probably meant something like

databaseRef.child(user.getUid()).setValue(displayName);

Or use a child key that cannot be null, as the error mentions

Also, not sure if that's necessary since you're already doing a profile update for the current user

John Luko
4#
John Luko Reply to 2018-01-13 01:17:47Z

Okey guys thank you everyone for the try. @cricket_007 made me think better. I already had the username so just changed this databaseRef.child(user.getDisplayName()).setValue(displayName); to this
databaseRef.push().child(displayName).setValue(null); Cant understand why it fails to read the username when using user.getDisplayName() on the same block of code but it worked that way.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.330785 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO