Hadi Samadbin Hadi Samadbin - 19 days ago 5
Android Question

null pointer exception on addmarker and markeroptions on googleMap

I get nullpointerException when trying to load google map on our app as below.also The java class extends from

AppCompatActivty
and 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) {
}
}

Answer

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: