Android - Null Reference for marker.setIcon()
<p><strong>EDIT</strong>: someone tagged it as a duplicate, but it's quite clear that this is not the case, but mostly an API bug.</p>

<p>I've encountered a strange problem using Maps.</p>

<p>First of all my code:</p>

<pre><code>@Override
public void onListUpdated() {
    // Cleaning all the markers.
    if (mGoogleMap != null) {
        mGoogleMap.clear();
    }

    List&lt;PetrolStation&gt; petrolStationList = mPetrolStationsList.getList();
    final HashMap&lt;Marker, PetrolStation&gt; markerPetrolStationHashMap = new HashMap&lt;&gt;();

    // Get the fuel selected by user.
    String fuelPrefs = loadPreferences(SETTINGS_PREFERENCES, FUEL_KEY);
    long fuelId = Long.valueOf(fuelPrefs);

    for (PetrolStation petrolStation : petrolStationList) {
        double lat = petrolStation.getLat();
        double lon = petrolStation.getLon();

        if (mGoogleMap != null) {
            Marker marker = mGoogleMap.addMarker(new MarkerOptions().position(new LatLng(lat, lon)));

            PROBLEM IS HERE -&gt; marker.setIcon(BitmapDescriptorFactory.fromResource(getMarkerIcon(fuelId)));
            markerPetrolStationHashMap.put(marker, petrolStation);
        }
    }

    ...

}
</code></pre>

<p>This is a method of an interface I've declared.</p>

<p>Testing the code on phones with older APIs everything is working fine, but using an emulator with API v23, it crashes with this log message:</p>

<pre><code>java.lang.NullPointerException: null reference
at maps.w.c.a(Unknown Source)
at maps.ad.g$a.&lt;init&gt;(Unknown Source)
at maps.ad.g.a(Unknown Source)
at maps.ad.S.a(Unknown Source)
at abq.onTransact(:com.google.android.gms.DynamiteModulesB:204)
at android.os.Binder.transact(Binder.java:387)
at com.google.android.gms.maps.model.internal.zzf$zza$zza.zzak(Unknown Source)
PROBLEM IS HERE -&gt; at com.google.android.gms.maps.model.Marker.setIcon(Unknown Source)
at com.myfuel.fragments.MapFragment.onListUpdated(MapFragment.java:317)
at com.myfuel.utils.PetrolStationsList$PetrolStationsAsyncTask.onPostExecute(PetrolStationsList.java:229)
at com.myfuel.utils.PetrolStationsList$PetrolStationsAsyncTask.onPostExecute(PetrolStationsList.java:151)
...
</code></pre>

<p>I've pointed out the line causing the crash in my code and the reference inside of the log.</p>

<p><a href="https://code.google.com/p/gmaps-api-issues/issues/detail?id=7696" rel="nofollow">Reading around</a> I've found out this is probably a bug (not fixed after all this time), but it's quite annoying and I hope someone will point me out some kind of solution.</p>

<p>Thank you.</p>

<p>EDIT:</p>

<pre><code>public static int getMarkerIcon(long fuelId) {
    int drawableId = -1;

    switch ((int) fuelId) {

        case 1: {
            drawableId = R.drawable.marker_petrol;

            break;
        }

        case 2: {
            drawableId = R.drawable.marker_special_petrol;

            break;
        }

        case 3: {
            drawableId = R.drawable.marker_lpg;

            break;
        }

        case 4: {
            drawableId = R.drawable.marker_diesel_image;

            break;
        }

        case 5: {
            drawableId = R.drawable.marker_special_diesel;

            break;
        }

        case 6 : {
            drawableId = R.drawable.marker_methane;

            break;
        }
    }

    return drawableId;
}
</code></pre>