Firebase onDataChange-空对象引用

时间:2019-03-17 06:26:56

标签: java android firebase

我想问问是否有人可以为我修复此问题。.因为我一直在搜索并尝试自己修复它,..不是。

这是我的logcat错误:

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)

这是我的“代码”:

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)是行代码 .title(rider.getName())

哦..如果还有其他可以为您提供的服务,您需要我尽快将其发布。

请理解..我有点想自己拿这些东西,但是,不能做..我只是被卡住了,即使它很简单:( ..

谢谢。

1 个答案:

答案 0 :(得分:1)

在此代码行之前添加“ if(dataSnapshot.exists())”。开始工作:)

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