10

I am using Firebase and have a signup / login activities working fine but I also want each user to update a username. It seems I am unable to run:

firebaseRef.getAuth().getUid()

I am receiving an error of within the app emulator "unfortunately, app has stopped".

This looks like the main error I am getting in android studio in the monitor: Something to do with the getUid().

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.firebase.client.AuthData.getUid()' on a null object reference

Here is my activity code:

package me.netsync.netconnect;

import android.app.ActionBar;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.firebase.client.AuthData;
import com.firebase.client.Firebase;

public class SettingActivity extends AppCompatActivity {

    private Firebase mRef;
    private Button BtnSettings;
    private EditText TxtUsername;

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

        mRef = new Firebase(Constants.FIREBASE_URL);

        BtnSettings = (Button)findViewById(R.id.BtnSettings);
        BtnSettings.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                // username txt update the profile
                TxtUsername = (EditText)findViewById(R.id.TxtUsername);
                String TxtUsernameStr = TxtUsername.getText().toString();
                TxtUsernameStr = TxtUsernameStr.trim();

                // check we can get a userid
               /* if(mRef.getAuth() == null){
                    Intent intent = new Intent(SettingActivity.this,LoginActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                    startActivity(intent);
                }*/

                if(TxtUsernameStr.isEmpty()){
                    // show a dialog box
                    AlertDialog.Builder builder = new AlertDialog.Builder(SettingActivity.this);
                    builder.setMessage("Please make sure you enter a Username" + TxtUsernameStr)
                            .setTitle("Error!")
                            .setPositiveButton(android.R.string.ok, null);
                    AlertDialog dialog = builder.create();
                    dialog.show();
                }else{
                    Firebase upDateUserProfile = new Firebase(Constants.FIREBASE_URL);
                    String uid = upDateUserProfile.getAuth().getUid();
                    User user = new User(TxtUsernameStr);
                    upDateUserProfile.child("users").child(uid).child("username").setValue(user);
                }
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        int btn = item.getItemId();

        if(btn == R.id.action_logout){
            mRef.unauth();
            Intent intent = new Intent(this,LoginActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
            startActivity(intent);
        }

        if(btn == R.id.action_settings){
            mRef.unauth();
            Intent intent = new Intent(this,SettingActivity.class);
            startActivity(intent);
        }

        return true;
    }
}

Any pointers appreciated.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
user3200080
  • 135
  • 1
  • 2
  • 9
  • Just to confirm, I am using the simple email/password login from Firebase. – user3200080 Jun 12 '16 at 10:54
  • [What is a null reference](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – noev Jun 12 '16 at 11:01
  • That means that AuthData is null, are you sure user is logged? Try checking if getAuth is null. [See this link](https://www.firebase.com/docs/android/guide/user-auth.html) Furthermore, I recommend you using new Firebase sdk. – Borja Jun 12 '16 at 11:17
  • I should be using new Frebase SDK. i installed it through studio, under "cloud" setting. – user3200080 Jun 12 '16 at 12:15
  • It seems once I have authenticated in my loginActivity, I am starting a new intent (mainActivity). When I click the menu item to go to my settingActivity the user is not authenticated. How do I maintain authentication across activities? – user3200080 Jun 12 '16 at 13:26
  • If you used the Cloud option, you're likely using Firebase 2.x. Did you create your project on firebase.google.com? If so, you cannot use the Firebase 2.x Authentication SDK and you should instead use the SDK documented here: https://firebase.google.com/docs/auth/ – Frank van Puffelen Jun 12 '16 at 15:10
  • I am experiencing this as well... and am following the docs, I am using version 9.4.0 not sure if that is an issue? My code is here http://stackoverflow.com/questions/40309345/firebase-uid-always-null – Lion789 Oct 28 '16 at 18:36
  • "Any pointers appreciated" I hope this was meant as a subtle pun... if so kudos – darkpbj Nov 11 '17 at 03:53

5 Answers5

4

In your app build.gradle, add this to your dependencies

compile 'com.google.firebase:firebase-auth:9.4.0'

import com.google.firebase.auth.FirebaseAuth;
...
...

if (FirebaseAuth.getInstance().getCurrentUser() == null) {
    //Go to login 
}
else{
    String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
}
Apex
  • 206
  • 2
  • 5
  • I have added an if else but then it is always null... http://stackoverflow.com/questions/40309345/firebase-uid-always-null – Lion789 Oct 28 '16 at 18:35
  • 1
    Why does it always show WARNING Method invocation 'getUid' may produce 'java.lang.NullPointerException' even when you checked that FirebaseAuth.getInstance().getCurrentUser() is not null? – Slick Slime May 15 '18 at 17:22
1

Need to add AuthStateListener.

        mAuth=FirebaseAuth.getInstance();
        mAuthListener=new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {


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

               //Your action here

            }

        }
    };
Mohammed Javad
  • 163
  • 1
  • 11
1

FirebaseAuth.getInstance().getUid(); This API seems to have been removed in Firebase 12.0.0

Use this instead FirebaseAuth.getInstance().getCurrentUser().getUid();

with the appropriate null check, or use Firebase 12.0.1 where i now seems to have @Hide annotation

Marc
  • 442
  • 7
  • 15
0
  • Add this after the Main Activity class

    Firebase firebaseAuth;
    DatabaseReference reference;
    
  • Add anywhere in onCreate

    String usern;
    usern = firebaseAuth.getInstance().getCurrentUser().getUid();
    
  • Upload to Firebase to their repective children

    reference=FirebaseDatabase.getInstance()
              .getReference().child("Patients")
              .child("Patient" + usern).child("Pres");
    
rinold simon
  • 2,782
  • 4
  • 20
  • 39
Rohit
  • 1
  • 2
-1

A simple way to check if there is any update for Firebase dependency:

enter image description here

Anthony
  • 3,595
  • 2
  • 29
  • 38