Firebase queries

0

I am having an error when I perform queries in firebase, my code for querying 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'm getting the following error:

  

Exception java.lang.RuntimeException: Unable to start activity ComponentInfo {ucarparceiro.ucarapp.com.ucarparceiro / ucarparceiro.ucarapp.com.ucarparceiro.Activity.ActivityPrincipal.ActivityProfile}: 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.

Thank you in advance.

    
asked by anonymous 13.07.2017 / 15:51

1 answer

1

You're having this error because the onDataChange() method is asynchronous, so when you call child(cpf) , cpf is still null .

You would have to read the card inside% 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) {

        }
    });
    
24.02.2018 / 13:04