Android Firebase - cant get userid using getUid () - Error: by reference to null object

I use Firebase and work in registration / login mode, but I also want each user to update the username. I can't seem to run:

firebaseRef.getAuth().getUid() 

I get an error in the application emulator "unfortunately the application has stopped."

This looks like the main error I get in the Android studio on the monitor: something related to getUid ().

java.lang.NullPointerException: attempt to call the virtual method 'java.lang.String com.firebase.client.AuthData.getUid ()' to reference a null object

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.

+10
android firebase firebase-authentication
source share
5 answers

In your build.gradle application 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(); } 
+4
source share

You must add AuthStateListener.

  mAuth=FirebaseAuth.getInstance(); mAuthListener=new FirebaseAuth.AuthStateListener() { @Override public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) { if(firebaseAuth.getCurrentUser()==null){ //Your action here } } }; 
+1
source share

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

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

with appropriate zero checking, or use Firebase 12.0.1, where I now have the @Hide annotation

+1
source share
  • Add this after the main lesson.

     Firebase firebaseAuth; DatabaseReference reference; 
  • Add anywhere on onCreate

     String usern; usern = firebaseAuth.getInstance().getCurrentUser().getUid(); 
  • Upload to Firebase for your kids

     reference=FirebaseDatabase.getInstance() .getReference().child("Patients") .child("Patient" + usern).child("Pres"); 
0
source share

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

enter image description here

-one
source share

All Articles