0

MainActivity starts. It first call vehicleRequest_validate() then the Map Fragment. However when vehicleRequest_validate() was called where createMarker starts I am having a null pointer also, here mMapFragment.addMarker.. Any clues why??

MainActivity Class

VehicleRequest.vehicleRequest_validate();

mapFragment = new MapFragment(MainActivity.this, this);
        manager = getSupportFragmentManager();
        manager.beginTransaction().replace(R.id.mainLayout, mapFragment).commit();

VehicleRequest Class

    call.enqueue(new Callback<JsonElement>() {
            @Override
            public void onResponse(Call<JsonElement> call, Response<JsonElement> response) {
                // success response
                if(response.body().isJsonArray()){
                    JsonArray objectWhichYouNeed = response.body().getAsJsonArray();
                    System.out.println(objectWhichYouNeed);

                    for(int i = 0; i< response.body().getAsJsonArray().size(); i++){
                        JsonElement lat_array = response.body().getAsJsonArray().get(i);
                        JsonObject lat_obj = lat_array.getAsJsonObject();
                        Double lat = lat_obj.get("lat").getAsDouble();

                        JsonElement lng_array = response.body().getAsJsonArray().get(i);
                        JsonObject lng_obj = lng_array.getAsJsonObject();
                        Double lng = lng_obj.get("lng").getAsDouble();

                        createMarker(lat, lng);
}

public static void createMarker(Double latitude, Double longitude) {
    BitmapDescriptor image = BitmapDescriptorFactory.fromResource(R.drawable.bus);

    mMapFragment.addMarker(new MarkerOptions()
            .position(new LatLng(latitude, longitude))
            .anchor(0.5f, 0.5f)
            .title("title")
            .snippet("snippet")
            .icon(image));

and then my MapFragment

Android Player_Shree
  • 1,212
  • 12
  • 31
  • The exception is probably due to `mMapFragment` being null. See https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it – akash93 Aug 02 '17 at 05:45
  • Hi akash93, mMapFragment was already been called at MapFragment like `public void onMapReady(GoogleMap googleMap) { mMapFragment = googleMap;` –  Aug 02 '17 at 06:01
  • That's not part of the code you posted so I have no way of knowing what exactly is null. See the question I linked to understand the stack trace and figure out what's causing the null pointer – akash93 Aug 02 '17 at 06:18
  • Hi akash, already fix it. Thanks. –  Aug 02 '17 at 06:45

0 Answers0