Skip to content Skip to sidebar Skip to footer

Null For Argument 'pathstring'

I am having an error when I perform queries in firebase, my code for query looks like this: @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedIns

Solution 1:

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(newValueEventListener() {
            @OverridepublicvoidonDataChange(DataSnapshot dataSnapshot) {
             cpf = dataSnapshot.getValue(String.class);
                txtCpf.setText(cpf);
            }

            @OverridepublicvoidonCancelled(DatabaseError databaseError) {

            }
        });



   DatabaseReference mReferencePlaca = FirebaseDatabase.getInstance().getReference();
    mReferencePlaca.child("Funcionario").child(cpf).child("placa")
            .addValueEventListener(newValueEventListener() {
                @OverridepublicvoidonDataChange(DataSnapshot dataSnapshot) {
                    placa = dataSnapshot.getValue(String.class);
                    txtPlaca.setText(placa);
                }

                @OverridepublicvoidonCancelled(DatabaseError databaseError) {

                }
            });

to this :-

DatabaseReference mReferenceCpf = FirebaseDatabase.getInstance().getReference();
databaseReference.child("Usuario").child(uId).child("cnpjCpf")
        .addValueEventListener(newValueEventListener() {
            @OverridepublicvoidonDataChange(DataSnapshot dataSnapshot) {
             cpf = dataSnapshot.getValue(String.class);
                txtCpf.setText(cps);

       DatabaseReference mReferencePlaca = FirebaseDatabase.getInstance().getReference();
        mReferencePlaca.child("Funcionario").child(cpf).child("placa")
                .addValueEventListener(newValueEventListener() {
                    @OverridepublicvoidonDataChange(DataSnapshot dataSnapshot) {
                        placa = dataSnapshot.getValue(String.class);
                        txtPlaca.setText(placa);
                    }

                    @OverridepublicvoidonCancelled(DatabaseError databaseError) {

                    }
                });
            }

            @OverridepublicvoidonCancelled(DatabaseError databaseError) {

            }
        });

Solution 2:

To solve your problem, just declare nome, cpf and placa Strings inside the coresponding onDataChange() methods, otherwise is null, due the asynchronous behaviour.

As an example, please use this code:

publicvoidonDataChange(DataSnapshot dataSnapshot) {
    String nome = dataSnapshot.getValue(String.class);
    TextView txtNome = (TextView) findViewById(R.id.txtNomePerfil);
    txtNome.setText(nome);
}

In the same way, you need to make the other changes.

Solution 3:

private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;

In the onCreate() method, initialize the FirebaseAuth instance and the AuthStateListener method so you can track whenever the user signs in or out.

mAuth = FirebaseAuth.getInstance();


mAuthListener = newFirebaseAuth.AuthStateListener() {
@OverridepublicvoidonAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
    FirebaseUseruser= firebaseAuth.getCurrentUser();
    if (user != null) {
        // User is signed in
        Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
    } else {
        // User is signed out
        Log.d(TAG, "onAuthStateChanged:signed_out");
    }
    // ...
}

};

Attach the listener to your FirebaseAuth instance in the onStart() method and remove it on onStop().

  public void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);

}

public void onStop() {
super.onStop();
if (mAuthListener != null) {
    mAuth.removeAuthStateListener(mAuthListener);
}

}

https://firebase.google.com/docs/auth/android/start/?hl=es-419

Solution 4:

not sure if this solution will be helpful for others.

background : I was setting firebase path value with intent parcel.

Solution : resolved this issue by updating androidmenifest as adding 'singleInstance'

<activityandroid:name=".yourActivity"android:launchMode="singleInstance"android:parentActivityName=".MainActivity">

Post a Comment for "Null For Argument 'pathstring'"