Consultations with Firebase

Asked

Viewed 126 times

0

I have an error when I perform the queries in firebase, my code for consultation is as follows:

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

    txtNome = (TextView) findViewById(R.id.txtNomePerfil);
    txtCpf = (TextView) findViewById(R.id.txtCpfPerfil);
    txtEmail = (TextView) findViewById(R.id.txtEmailPerfil);
    txtFone = (TextView) findViewById(R.id.txtCelularPerfil);
    txtPlaca = (TextView) findViewById(R.id.txtPlacaPerfil);
    imgPerfil = (ImageView) findViewById(R.id.imgPerfil);
    txtNomeFantasia = (TextView) findViewById(R.id.txtNomeFantasiaPerfil);
    txtCnpj = (TextView) findViewById(R.id.txtCnpjPerfil);

    firebaseUser = firebaseAuth.getInstance().getCurrentUser();

    uId = firebaseUser.getUid();
    email = firebaseUser.getEmail();
    txtEmail.setText(email);

    databaseReference = FirebaseDatabase.getInstance().getReference();

    databaseReference.child("Usuario").child(uId).child("nome")
            .addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
          nome = dataSnapshot.getValue(String.class);
            txtNome.setText(nome);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });


   DatabaseReference mReferenceCpf = 
FirebaseDatabase.getInstance().getReference();
    databaseReference.child("Usuario").child(uId).child("cnpjCpf")
            .addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
         cpf = dataSnapshot.getValue(String.class);
            txtCpf.setText(cpf);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });



    DatabaseReference mReferencePlaca = 
FirebaseDatabase.getInstance().getReference();
    mReferencePlaca.child("Funcionario").child(cpf).child("placa")
            .addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    placa = dataSnapshot.getValue(String.class);
                    txtPlaca.setText(placa);
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });

However I am getting the following error:

Exception java.lang.RuntimeException: Unable to start activity ComponentInfo{ucarparceiro.ucarapp.com.ucarparceiro/ucarparceiro.ucarapp.com.ucarparceiro.Activity.ActivityPrincipal.ActivityPerfil}: java.lang.NullPointerException: Can't pass null for argument 'pathString' in child() android.app.Activitythread.performLaunchActivity (Activitythread.java:2316) android.app.Activitythread.handleLaunchActivity (Activitythread.java:2376) android.app.Activitythread.access$800 (Activitythread.java:147) android.app.Activitythread$H.handleMessage (Activitythread.java:1281) android.os.Handler.dispatchMessage (Handler.java:102) android.os.Looper.loop (Looper.java:135) android.app.Activitythread.main (Activitythread.java:5253) java.lang.reflect.Method.invoke (Method.java) java.lang.reflect.Method.invoke (Method.java:372) com.android.Internal.os.Zygoteinit$Methodandargscaller.run (Zygoteinit.java:899) com.android.Internal.os.Zygoteinit.main (Zygoteinit.java:694)

The data in Firebase is as follows. Nó Usuário Nó Funcionario

Thanks in advance.

1 answer

1

You’re having this mistake because the method onDataChange() is asynchronous, it means that when you call the child(cpf), the cpf is still null.

You’d have to read the sign inside the onDataChange() of the Cpf:

DatabaseReference mReferenceCpf = 
FirebaseDatabase.getInstance().getReference();
    databaseReference.child("Usuario").child(uId).child("cnpjCpf")
            .addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            cpf = dataSnapshot.getValue(String.class);
            txtCpf.setText(cpf);
            DatabaseReference mReferencePlaca = 
                FirebaseDatabase.getInstance().getReference();
            mReferencePlaca.child("Funcionario").child(cpf).child("placa")
                .addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        placa = dataSnapshot.getValue(String.class);
                        txtPlaca.setText(placa);
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

Browser other questions tagged

You are not signed in. Login or sign up in order to post.