null for argument 'pathString'

后端 未结 4 439
我寻月下人不归
我寻月下人不归 2021-01-20 10:25

I am having an error when I perform queries in firebase, my code for query looks like this:

@Override
protected void onCreate(Bundle savedInstanceState) {
           


        
4条回答
  •  北海茫月
    2021-01-20 10:54

    You're most likely getting an error because cpf is null when you use it as a child in mReferenceplaca. This happens because Firebase downloads data asynchronously and your code lines are executed synchronously. Therefore, by the time this line mReferencePlaca.child("Funcionario").child(cpf).child("placa") is executed, the value of cpf is still null because this cpf = dataSnapshot.getValue(String.class); hasn't happened yet.

    To solve this, change this :-

        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) {
    
                    }
                });
    

    to this :-

    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(cps);
    
           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) {
    
                }
            });
    

提交回复
热议问题