简体   繁体   中英

Firebase onDataChange - null object reference

I'd like to ask if someone can fix this for me.. cuz i've been searching and trying to fix it myself but .. nope..

here's my logcat error:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.app.mk.transportrider, PID: 23152
    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.app.mk.transportrider.Model.Rider.getName()' on a null object reference
        at com.app.mk.transportrider.Home$5$1.onDataChange(Home.java:364)
        at com.google.firebase.database.zzp.onDataChange(Unknown Source:7)
        at com.google.android.gms.internal.firebase_database.zzfc.zza(Unknown Source:13)
        at com.google.android.gms.internal.firebase_database.zzgx.zzdr(Unknown Source:2)
        at com.google.android.gms.internal.firebase_database.zzhd.run(Unknown Source:71)
        at android.os.Handler.handleCallback(Handler.java:789)
        at android.os.Handler.dispatchMessage(Handler.java:98)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6944)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

and here is my "code":

DatabaseReference driverLocation = FirebaseDatabase.getInstance().getReference(Common.driver_tbl);
GeoFire gf = new GeoFire(driverLocation);

GeoQuery geoQuery = gf.queryAtLocation(new GeoLocation(mLastLocation.getLatitude(), mLastLocation.getLongitude()), distance);
geoQuery.removeAllListeners();


geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
    @Override
    public void onKeyEntered(String key, final GeoLocation location) {


        FirebaseDatabase.getInstance().getReference(Common.user_driver_tbl)
                .child(key)
                .addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {

                        // Same model *users* & *driver*
                        // using the ssame model ---
                        Rider rider = dataSnapshot.getValue(Rider.class);

                        mMap.addMarker(new MarkerOptions()
                                .position(new LatLng(location.latitude, location.longitude))
                                .flat(true)
                                .title(rider.getName())
                                .snippet("Phone: " + rider.getPhone())
                                .icon(BitmapDescriptorFactory.fromResource(R.drawable.car)));
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });

(Home.java:364) is the line code .title(rider.getName())

Ough.. and if there is something else i could provide you more, that you need i'll post it as soon as i can.

And please understand.. i'm kinda trying to get these things by myself but, no can do .. i just got stuck even if it is that simple :( ..

Thank you.

Adding "if(dataSnapshot.exists())" before this line of code.. started working :)

Rider rider = dataSnapshot.getValue(Rider.class);

                        mMap.addMarker(new MarkerOptions()
                                .position(new LatLng(location.latitude, location.longitude))
                                .flat(true)
                                .title(rider.getName())
                                .snippet("Phone: " + rider.getPhone())
                                .icon(BitmapDescriptorFactory.fromResource(R.drawable.car)));

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