Firebase数据库引用在初始化时采用null值

时间:2017-05-24 09:59:41

标签: android firebase firebase-realtime-database

Click here for screenshot Firebase数据库正在进行空引用。 请建议捕获零点异常的方法。 我添加了google-services.json并对build.gradle文件进行了更改。 我初始化了databasereference。我附加调试器屏幕和代码。

package com.monil.ngonearby;

 import android.util.Log;

 import com.google.firebase.database.DatabaseReference;
 import com.google.firebase.database.FirebaseDatabase;

   import org.json.JSONArray;
  import org.json.JSONException;
  import org.json.JSONObject;

   import java.util.ArrayList;
   import java.util.HashMap;
     import java.util.List;


    public class DataParser {
    DatabaseReference  databaseReference;
 String id =null;


public List<HashMap<String, String>> parse(String jsonData) {
    JSONArray jsonArray = null;
    JSONObject jsonObject;


    try {
        Log.d("Places", "parse");
        jsonObject = new JSONObject((String) jsonData);
        jsonArray = jsonObject.getJSONArray("results");
    } catch (JSONException e) {
        Log.d("Places", "parse error");
        e.printStackTrace();
    }
    return getPlaces(jsonArray);
}

private List<HashMap<String, String>> getPlaces(JSONArray jsonArray) {
    int placesCount = jsonArray.length();
    List<HashMap<String, String>> placesList = new ArrayList<>();
    HashMap<String, String> placeMap = null;
    Log.d("Places", "getPlaces");

    for (int i = 0; i < placesCount; i++) {
        try {
            placeMap = getPlace((JSONObject) jsonArray.get(i));
            placesList.add(placeMap);
            Log.d("Places", "Adding places");

        } catch (JSONException e) {
            Log.d("Places", "Error in Adding places");
            e.printStackTrace();
        }
    }
    return placesList;
}

private HashMap<String, String> getPlace(JSONObject googlePlaceJson) {

    HashMap<String, String> googlePlaceMap = new HashMap<String, String>();
    String placeName = "-NA-";
    String vicinity = "-NA-";
    String latitude = "";
    String longitude = "";
    String reference = "";
    String placeid= "-NA-";
    //String formatted_phone_number = " ";

    Log.d("getPlace", "Entered");

    try {
        if (!googlePlaceJson.isNull("name")) {
            placeName = googlePlaceJson.getString("name");
        }
        if (!googlePlaceJson.isNull("vicinity")) {
            vicinity = googlePlaceJson.getString("vicinity");
        }

            placeid = googlePlaceJson.getString("place_id");
            StringBuilder googlePlacesUrl = new StringBuilder("https://maps.googleapis.com/maps/api/place/details/json?");
            googlePlacesUrl.append("placeid=" + placeid);
            googlePlacesUrl.append("&key=" + "MY API KEY");
        latitude = googlePlaceJson.getJSONObject("geometry").getJSONObject("location").getString("lat");
        longitude = googlePlaceJson.getJSONObject("geometry").getJSONObject("location").getString("lng");
        reference = googlePlaceJson.getString("reference");

           String url =(String) googlePlacesUrl.toString();
        Object[] DataTransfer = new Object[1];

        DataTransfer[0] = url;
        GetNearbyPlacesData1 getNearbyPlacesData = new GetNearbyPlacesData1();



           getNearbyPlacesData.execute(DataTransfer);



        googlePlaceMap.put("place_name", placeName);
        googlePlaceMap.put("vicinity", vicinity);
        googlePlaceMap.put("lat", latitude);
        googlePlaceMap.put("lng", longitude);
        googlePlaceMap.put("reference", reference);
        NgoDetails ngoDetails = new NgoDetails(placeName , vicinity);

        databaseReference = FirebaseDatabase.getInstance().getReference("update") ;
       id = databaseReference.push().getKey();


       databaseReference.child(id).setValue(ngoDetails);

            Log.d("getPlace", "Putting Places");
    } catch (JSONException e) {
        Log.d("getPlace", "Error");
        e.printStackTrace();
    }
    return googlePlaceMap;
}

public void getresults(String googlePlacesData) throws JSONException {

    JSONObject jsonObject1 = new JSONObject((String) googlePlacesData);
    JSONObject result = jsonObject1.getJSONObject("result");
    String formatted_phone_number = result.getString("formatted_phone_number");
   // NgoDetails ngoDetails1 = new NgoDetails(formatted_phone_number);
   // databaseReference.child(id).child("phoone").setValue(formatted_phone_number);
   // googlePlaceMap.put("formatted_phone_number", formatted_phone_number);

}

 }

1 个答案:

答案 0 :(得分:0)

试试这个

databaseReference = FirebaseDatabase.getInstance().getReference().child("update");

n告诉我