Hadi Samadbin Hadi Samadbin - 1 year ago 180
Android Question

NullPointerException When Trying to place Marker on Google Map V2

when I try to place Marker on google map with placeMarker method I get NullPointerException

NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference`
I/DpmTcmClient: RegisterTcmMonitor from: org.apache.http.impl.conn.TcmIdleTimerMonitor
W/DynamiteModule: Local module descriptor class for com.google.android.gms.googlecertificates not found.
I/DynamiteModule: Considering local module com.google.android.gms.googlecertificates:0 and remote module com.google.android.gms.googlecertificates:2
I/DynamiteModule: Selected remote version of com.google.android.gms.googlecertificates, version >= 2
D/GoogleCertificates: com.google.android.gms.googlecertificates module is loaded
E/url: http://maps.googleapis.com/maps/api/geocode/json?latlng=35.7742618,51.3596331&sensor=true
E/Lat Long to Addresss:
E/Googlemap: null
D/GoogleCertificatesImpl: Fetched 363 Google certificates
I/b: Sending API token request.
E/b: Authentication failed on the server.
E/Google Maps Android API: Authorization failure
Ensure that the "Google Maps Android API v2" is enabled.
Ensure that the following Android Key exists:

Also I used getAsyncMap and OnMapReady().Inside OnMapReady I invoke an Asynktask to get user location and onPostExecute placeMarker Method is invoked:

public void placeMarker(LatLongDetails user_latlongobj2,
final Context contextPlace) {

try {
if (googlemap == null) {
if (LoginDetails.Address.length() < 1) {
LoginDetails.Address = "Getting your location .....";
marker = new MarkerOptions().position(
new LatLng(user_latlongobj2.user_latitude,

System.out.println("This is the Address" + LoginDetails.Address);


System.out.println("PLACING MARKER" + LoginDetails.Address);
if (marker == null || contextPlace == null) {
Intent in =new Intent(this,ProfileActivity1.class);
fixmarker(marker, contextPlace);

} catch (Exception e) {
fixmarker(marker, contextPlace);
Log.d("Profileactivity", "" + e);


All these problem came along when I tried to change getMap() to getAsyncMap() Also pls check This link And the complete Whole Activity on GitHub

I removed try and catch to find more details about error.here is logcat:

enter code herejava.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
at android.content.ContextWrapper.getResources(ContextWrapper.java:87)
at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:81)
at android.support.v7.app.AppCompatActivity.getResources(AppCompatActivity.java:551)
at android.content.Context.getString(Context.java:413)
at com.example.cabbookinghome.ProfileActivity1.intialiseMap(ProfileActivity1.java:280)
at com.example.cabbookinghome.ProfileActivity1.placeMarker(ProfileActivity1.java:327)
at epbit.helper.ConversionTaskLatLonLoc.onPostExecute(ConversionTaskLatLonLoc.java:130)
at epbit.helper.ConversionTaskLatLonLoc.onPostExecute(ConversionTaskLatLonLoc.java:24)
at android.os.AsyncTask.finish(AsyncTask.java:651)
at android.os.AsyncTask.access$500(AsyncTask.java:180)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
at android.os.Handler.dispatchMessage(Handler.java:102)

Answer Source

After 3 Days Searching and Working on it here is what finally worked.the real problem is googlemap is null which means map is not loaded.I used try and catch but You should never just catch and exception and do nothing. after Log the exceptions, check the logs I found this:

com.google.android.gms.maps.MapFragment.getMap()' on a null object reference

First I tried This link Google Map returning nullpointerexception Google Maps Android V2

googleMap = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map)).getMap();

Then getChildFragmentManager become red indicating getChildFragmentManager () cannot be resolved or cannot be referenced.using getSupportFragmentManager() instead as described in this link (getChildFragmentManager () cannot be resolved or cannot be referenced) finally solved the problem and map has been loaded

googlemap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapfragment)).getMap();

Note that if your class extends from AppCompatActivitythen You need to use getSupportFragmentManager() because getChildFragmentManager() is a method of a Fragment not accessible fromAppCompatActivity

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download