I am having an error when I perform queries in firebase, my code for query looks like this:
@Override
protected void onCreate(Bundle savedInstanceState) {
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) {
}
});