Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 416
user3200080 Avatar asked Jun 12 '16 10:06

user3200080


1 Answers

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

like image 148
Marc Avatar answered Oct 26 '22 18:10

Marc