简体   繁体   中英

I am trying to retrieve data from Firebase Database but experiencing a NullPointerException error

The error I am getting is

java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.database.DatabaseReference com.google.firebase.database.DatabaseReference.child(java.lang.String)' on a null object reference

My code is:

    firebaseAuth = FirebaseAuth.getInstance();
    DR = FirebaseDatabase.getInstance().getReference().child("All Drivers");
    firebaseUser = firebaseAuth.getCurrentUser();
    firebaseDatabase = FirebaseDatabase.getInstance();
    mStorage = FirebaseStorage.getInstance().getReference();

    mReference.child("All Driver").child(FirebaseAuth.getInstance().getCurrentUser().getUid())
            .addValueEventListener( new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    String image;

                    image = dataSnapshot.child("userDp").getValue().toString();

                    Picasso.get().load(image).into(mProfile_image);
                }

What am I doing wrong?

Your mReference is null. You should make sure it's assigned to an actual object before calling methods on it. From what you've shown, it looks like you never assigned it, which means it has the default value of null.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM