1

I get nullpointerException when trying to load google map on our app as below.also The java class extends from AppCompatActivtyand inside intialiseMap() we try to getMap()

D/Profileactivity: 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

And here is where we get the exception,inside placeMarker method

public void placeMarker(LatLongDetails user_latlongobj2,
                        final Context contextPlace) {
    try {
        if (googlemap == null) {
            intialiseMap();
            animatecamera(user_latlongobj);
        }
        if (LoginDetails.Address.length() < 1) {
            LoginDetails.Address = "Getting your location .....";
        }
        //googlemap.clear();
        marker = new MarkerOptions().position(
                new LatLng(user_latlongobj2.user_latitude,
                        user_latlongobj2.user_longitude)).title(
                LoginDetails.Address);

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

        marker.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker));

        if (googlemap != null) {

            googlemap.addMarker(marker).showInfoWindow();
        }else {
            intialiseMap();
            googlemap.addMarker(marker).showInfoWindow();
        }
        System.out.println("PLACING MARKER" + LoginDetails.Address);
        if (marker == null || contextPlace == null) {
            Intent in =new Intent(this,ProfileActivity1.class);
            in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            in.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(in);
        }
        else
            fixmarker(marker, contextPlace);

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

}

This is how we initialise Map

private void intialiseMap() {

try {
if (dialog == null)
    dialog = ProgressDialog.show(ProfileActivity1.this, "",
            getString(R.string.getting_location), true, true);
}catch(Exception e) {
Log.e("eybaba",""+e);
}
    try {
        if (googlemap == null) {
            googlemap = ((MapFragment) getFragmentManager()
                    .findFragmentById(R.id.mapfragment)).getMap();
            googlemap.setInfoWindowAdapter(new CustomInfowindow(context));
            // check if map is created successfully or not
            if (googlemap == null) {
                Toast.makeText(getApplicationContext(),
                        R.string.maps_create_error, Toast.LENGTH_SHORT)
                        .show();
            }
        }
    } catch (Exception e) {
    }
}
Hadi Samadbin
  • 237
  • 3
  • 17
  • 1
    I'd guess that `intialiseMap();` isn't working or returns a new map and the call thus should be `googlemap = intialiseMap();`. Basically the error message tells you what's wrong: `NullPointerException`. – Thomas Nov 17 '16 at 10:10
  • The first warning shows that `user_longitude` in class `LatLongDetails` is a class variable that you're accessing via an instance `user_latlongobj2.user_longitude`, but has a `static` modifier. You can simply remove `static`. – QBrute Nov 17 '16 at 10:12
  • @Thomas google= intialiseMap(); gives incompatible types error – Hadi Samadbin Nov 17 '16 at 10:13
  • `else { intialiseMap(); googlemap.addMarker(marker).showInfoWindow(); }` <-- this piece of code is executed, when `googlemap` is `null`. Referring to @Thomas comment, you need to initialize it in *some* way. – QBrute Nov 17 '16 at 10:15
  • @QBrute I added this line just before asking the question here to check if this willl solve the nullpointer exception – Hadi Samadbin Nov 17 '16 at 10:17
  • @QBrute removing stativ in class LatLongDetailes solve that.can u help to figure out null pointer exception.How can I intialise googleMap?google= intialiseMap(); gives incompatible types error – Hadi Samadbin Nov 17 '16 at 10:25
  • @HadiSamadbin Please post how you implemented `initialiseMap()`. – QBrute Nov 17 '16 at 10:28
  • @QBrute just added – Hadi Samadbin Nov 17 '16 at 10:32
  • You should never just catch and exception and do nothing. If `intialiseMap()` fails to initialize `googlemap` you might not notice it. Btw, of course `google= intialiseMap();` will fail if `intialiseMap()` returns `void` (that's why I said I was guessing - you didn't show code then) and `google=...` would _probably_ not work anyways since the variable's name is `googlemap`. – Thomas Nov 17 '16 at 11:01
  • @Thomas so what I need to to do Thomas? – Hadi Samadbin Nov 17 '16 at 11:14
  • Log the exceptions, check the logs and if you see a problem that you can't solve yourself ask about it. If there are no exceptions and `googlemap` still is null then debug your `initialiseMap()`. – Thomas Nov 17 '16 at 11:23
  • @Thomas tnx for ur help I loged the exception found the problem and seaching for the answer ,find it and the map is now loading – Hadi Samadbin Nov 17 '16 at 11:43
  • @Thomas Pls write you answer as you mentioned the problem is in intialiseMap com.google.android.gms.maps.MapFragment.getMap()' on a null object reference – Hadi Samadbin Nov 17 '16 at 11:50
  • These two Links Solved the problem 1.http://stackoverflow.com/questions/30116762/google-map-returning-nullpointerexception-google-maps-android-v2 2.http://stackoverflow.com/questions/35090867/getchildfragmentmanager-cannot-be-resolved-or-cannot-be-referenced – Hadi Samadbin Nov 17 '16 at 11:52
  • I'm glad it worked but besides giving tips on logging I didn't contribute much that's worth an answer :) – Thomas Nov 17 '16 at 12:00
  • @Thomas But your tips plus log exception and two stackover flow links can help many others...would u pls write an answer? if possible pls mention the two links and the exception for others in answer – Hadi Samadbin Nov 17 '16 at 13:18

1 Answers1

1

As requested an answer to summarize the solution and the process of finding it:

1) The NullPointerException and the according message indicate the problem is in the else-branch of this block:

    if (googlemap != null) {
        googlemap.addMarker(marker).showInfoWindow();
    }else {
        intialiseMap();
        googlemap.addMarker(marker).showInfoWindow();
    }

Here, intialiseMap(); seems to fail to initialize the map.

2) In intialiseMap() there's a try-catch-block where googlemap should be initialized if null. However, the catch-block is empty and thus any exception when trying the initialization gets lost.

Note for future readers: if you catch an exception you should always, always, always handle it in some way. One of the simples ways to at least do something is to log it.

Of course there are situations where you just want to ignore a specific exception but in that case you should really know the consequences (what happens when that exception is thrown, why is it thrown etc.) and you always should document that you're ignoring that exception on purpose, e.g. with a short comment in your code.

3) After logging the caught exceptions the OP realized that initialization of googlemap failed and thus was able to further track the problem.

He then searched for answers and solutions and came up with the following two threads which helped him solve his problem:

Community
  • 1
  • 1
Thomas
  • 87,414
  • 12
  • 119
  • 157