0

I keep getting this error: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.firebase.auth.FirebaseUser.getUid()' on a null object reference When I run my code:

import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;

import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import de.hdodenhof.circleimageview.CircleImageView;

public class SettingsActivity extends AppCompatActivity
{
    private CircleImageView settingsDisplayProfileImage;
    private TextView settingsDisplayname;
    private TextView settingsDisplayStatus;
    private Button setingsChangeProfileImage;
    private Button settingsChangeStatus;

    private DatabaseReference getUserDataReference;
    private FirebaseAuth mAuth;

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

        mAuth = FirebaseAuth.getInstance();
        String online_user_id = mAuth.getCurrentUser().getUid();

        getUserDataReference = FirebaseDatabase.getInstance().getReference().child("Users").child(online_user_id);

        settingsDisplayProfileImage = (CircleImageView) findViewById(R.id.settings_profile_image);
        settingsDisplayname = (TextView) findViewById(R.id.settings_username);
        settingsDisplayStatus = (TextView) findViewById(R.id.settings_user_status);
        setingsChangeProfileImage = (Button) findViewById(R.id.settings_change_profile_image_button);
        settingsChangeStatus = (Button) findViewById(R.id.settings_profile_status);


        getUserDataReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot)


            {
                String name = dataSnapshot.child("user_name").getValue().toString();
                String status = dataSnapshot.child("user_status").getValue().toString();
                String image = dataSnapshot.child("user_image").getValue().toString();
                String thumb_image = dataSnapshot.child("user_thumb_image").getValue().toString();


                settingsDisplayname.setText(name);
                settingsDisplayStatus.setText(status);

            }

            @Override
            public void onCancelled( DatabaseError databaseError) {

            }
        });

    }
}

I have tried a few solutions but nothing seems to work. This code seems to be the issue but I don't know how to resolve the error: String online_user_id = mAuth.getCurrentUser().getUid();

My logcat bring up this error: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.firebase.auth.FirebaseUser.getUid()' on a null object reference

Markus Kauppinen
  • 3,025
  • 4
  • 20
  • 30
  • 3
    getCurrentUser() returns null, you don't have an authenticated user at that moment. You need to sign in your user first: https://firebase.google.com/docs/auth/android/firebaseui – algrid Dec 13 '18 at 21:58
  • Every time I run the code I am logged in as a user. – Mfalme Droid Dec 13 '18 at 22:05
  • 1
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Tyler V Dec 14 '18 at 04:05
  • @MfalmeDroid did u get it to work? – sats Dec 16 '18 at 10:42

1 Answers1

0

Before you get the uid, check if the user is signed in. It's unsafe to assume that the user is authenticated every time the app runs, so it's a good practice to actually make sure you are signed in.

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
    // User is signed in
} else {
    // No user is signed in
}

Refer link for more information on managing users: Official doc

getCurrentUser() might also return null if auth object has not finished initialising. Use AuthStateListener to better handle this situation.

sats
  • 647
  • 6
  • 17