0

I am getting NullPointerException on addMarker line because my mGoogleMap Object is not getting initialized in onMapReady() although I have called getMapAsync().

Java File :

    public class RideStart extends AppCompatActivity
            implements NavigationView.OnNavigationItemSelectedListener,PlaceSelectionListener,OnMapReadyCallback,LocationListener,GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener
    {


        String phone,username,email,ridetrackingno,GPSstart,GPSend;
        private GoogleMap nMap;
        MapView nmapview;
        GoogleMap mGoogleMap;
        SupportMapFragment mapFrag;
        LocationRequest mLocationRequest;
        GoogleApiClient mGoogleApiClient;
        Location mLastLocation;
        Marker mCurrLocationMarker;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_ride_start);
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolBar);
            setSupportActionBar(toolbar);

            Log.d("Here!!","before initialization" );
            mapFrag= (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
            mapFrag.getMapAsync((OnMapReadyCallback) this);

            Log.d("Here!!","after initialization" );


........//addMarker code --->NULL-POINTER exception bcoz mGooglemap isnt initialized
}
//onCreate ends.

public void onMapReady(GoogleMap googleMap) {
        Log.d("Here!!","in initialization" );
        LatLng sydney = new LatLng(12.843515, 77.663306);
        googleMap.addMarker(new MarkerOptions().position(sydney)
                .title("You are here"));
        googleMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
        googleMap.animateCamera(CameraUpdateFactory.zoomTo(15), 2000, null);
        mGoogleMap=googleMap;
        mGoogleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    }


}
//class ends

Debugging Statements : clearly show "before initialization and "after initialization" but "in initialization" is never printed. I cannot understand why this is happening.

03-26 11:14:41.260 20046-20046/com.example.kulka.cabchain_partner D/Here!!: before initialization
03-26 11:14:41.262 20046-20046/com.example.kulka.cabchain_partner D/Here!!: after initialization
03-26 11:14:41.266 20046-20046/com.example.kulka.cabchain_partner D/AndroidRuntime: Shutting down VM
03-26 11:14:41.267 20046-20046/com.example.kulka.cabchain_partner E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                    Process: com.example.kulka.cabchain_partner, PID: 20046
                                                                                    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.kulka.cabchain_partner/com.example.kulka.cabchain_partner.RideStart}: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.model.Marker com.google.android.gms.maps.GoogleMap.addMarker(com.google.android.gms.maps.model.MarkerOptions)' on a null object reference
Vishal Chhodwani
  • 2,567
  • 5
  • 27
  • 40
tester9
  • 93
  • 9
  • It is async, so you have to write the code to add marker after the `onMapReady()` gets executed. Try to put the code in a function and call it at the end of `onMapReady()` – Sai Kishore Mar 26 '18 at 05:55

1 Answers1

1

adding marker on onCreate will always refers to null.. as the map is not ready

   @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_ride_start);
                Toolbar toolbar = (Toolbar) findViewById(R.id.toolBar);
                setSupportActionBar(toolbar);


            Log.d("Here!!","before initialization" );
            mapFrag= (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
            mapFrag.getMapAsync((OnMapReadyCallback) this);

            Log.d("Here!!","after initialization" );



}
//onCreate ends.

public void onMapReady(GoogleMap googleMap) {
        Log.d("Here!!","in initialization" );
        LatLng sydney = new LatLng(12.843515, 77.663306);
        googleMap.addMarker(new MarkerOptions().position(sydney)
                .title("You are here"));
        googleMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
        googleMap.animateCamera(CameraUpdateFactory.zoomTo(15), 2000, null);
        mGoogleMap=googleMap;
        mGoogleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);

        //make a function for adding marker
    }


}
//class ends

Remove the marker code from onCreate method and place it below the onMapReady method or simply make another method and call it at the end of onMapReady method.. This should do the trick