【问题标题】:Firebase onDataChange method on null object reference空对象引用上的 Firebase onDataChange 方法
【发布时间】:2016-05-19 21:54:25
【问题描述】:

这就是我想要的样子,但我得到的是空对象引用 在(DataSnapshot wayPointsChild : wayPointsSnapshotChild.getChildren()) {内部

这两个属性使一切崩溃

 "timeStamp" : "2016/04/27 18:28:52",
 "travelType" : "work"

JSON FROMAT

"waypoints" : {
        "-KHB1VjqUdO90vxj9XCh" : {

          "timeStamp" : "2016/04/27 18:28:52",
          "travelType" : "work"

          "-KHB1VjqUdO90vxj9XCi" : {
            "latitude" : 58.337679,
            "longitude" : 11.912757
          },
          "-KHB1ZykbwgXM9sPNie9" : {
            "latitude" : 58.281384,
            "longitude" : 12.294495
          },

        },
        "-KHpPe06hLpPttuGZ0rZ" : {

          "timeStamp" : "2016/04/27 18:28:52",
          "travelType" : "private"

          "-KHpPe07pE5ZYn4JGiZ1" : {
            "latitude" : 58.281384,
            "longitude" : 12.294495
          },
          "randomid1212" : {
            "latitude" : 57.689903,
            "longitude" : 11.989792
          },
          "randomid1213" : {
            "latitude" : 57.689905,
            "longitude" : 11.989795
          },

        }

当前的 onDataChange() 方法

mUserRef.addValueEventListener(new ValueEventListener() {
            //this keeps per-user list of points
            List<MyWaypoint> userWayPointsList = new ArrayList<MyWaypoint>();

            @Override
            public void onDataChange(DataSnapshot wayPointsDataSnapshot) {
                if (wayPointsDataSnapshot.getChildrenCount() > 0) {


                    for (DataSnapshot wayPointsSnapshotChild : wayPointsDataSnapshot.getChildren()) {
                        Log.i("FireBaseTester", "For-Loop :: wayPointsSnapshotChild.getValue() : " + wayPointsSnapshotChild.getValue());
                        if (wayPointsSnapshotChild.getChildrenCount() > 0) {


                            // Temporary list
                            List<String> latLngListTemp = new ArrayList<>();


                            // THIS Gives me the error because it get used in the next for loop for some reason.
                            wayPointsSnapshotChild.child("timeStamp").getValue();


                            for (DataSnapshot wayPointsChild : wayPointsSnapshotChild.getChildren()) {

                                //this is where we get the Lat and Lon
                                double latitude = Double.parseDouble(wayPointsChild.child("latitude").getValue().toString());
                                double longitude = Double.parseDouble(wayPointsChild.child("longitude").getValue().toString());
                                Log.i("FireBaseTester", "latitude = " + latitude + " , longitude = " + longitude);

                                latLngListTemp.add(latitude + ", " + longitude);

                            }

                            // List containing a nested List<List<String>>
                            latitudeNlongitude.add(latLngListTemp);
                        }

                    }

                }

                //here you can assign the points to the user
                Log.i("FireBaseTester", "There are " + userWayPointsList.size() + " Points for User");
            }

            @Override
            public void onCancelled(FirebaseError firebaseError) {
                Log.e("FireBaseTester", "onCancelled - wayPointRef Error is " + firebaseError.getMessage());
            }
        });
    } else {
        Log.i("FireBaseTester", "No WayPoints Data Received");
    }

LOGCAT

致命异常:主要 进程:com.example.rasmusjosefsson.rjcar,PID:25446 java.lang.NullPointerException:尝试调用虚拟方法 空对象上的“java.lang.String java.lang.Object.toString()” 参考 在 com.example.rasmusjosefsson.rjcar.demo.MapListActivityRealBack2$1.onDataChange(MapListActivityRealBack2.java:122) 在 com.firebase.client.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:56) 在 com.firebase.client.core.view.DataEvent.fire(DataEvent.java:45) 在 com.firebase.client.core.view.EventRaiser$1.run(EventRaiser.java:38) 在 android.os.Handler.handleCallback(Handler.java:739) 在 android.os.Handler.dispatchMessage(Handler.java:95) 在 android.os.Looper.loop(Looper.java:148) 在 android.app.ActivityThread.main(ActivityThread.java:5417) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

【问题讨论】:

    标签: android json firebase jackson firebase-realtime-database


    【解决方案1】:

    这些行说明了问题:

    wayPointsSnapshotChild.child("timeStamp").getValue();
    for (DataSnapshot wayPointsChild : wayPointsSnapshotChild.getChildren()) {
    { 
      double latitude = Double.parseDouble(wayPointsChild.child("latitude").getValue().toString());
    

    wayPointSnapshotChild 上调用getChildren 将返回timestamp 值作为其中一个子级,该子级没有名为latitude 的子级。

    您可以像这样构建数据:

    "waypoints" : {
        "-KHB1VjqUdO90vxj9XCh" : {
    
          "timeStamp" : "2016/04/27 18:28:52",
          "travelType" : "work"
          "points": {
              "-KHB1VjqUdO90vxj9XCi" : {
                "latitude" : 58.337679,
                "longitude" : 11.912757
              },
              "-KHB1ZykbwgXM9sPNie9" : {
                "latitude" : 58.281384,
                "longitude" : 12.294495
              }
          }
        }
    

    然后在您的for 循环中调用它:

    wayPointsSnapshotChild.child("points").getChildren()
    

    那么内部循环将只接收包含纬度和经度属性的 wayPointChild 对象。

    【讨论】:

    • 谢谢伙计!像魅力一样工作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-03
    • 1970-01-01
    • 2022-08-21
    相关资源
    最近更新 更多