Mapbox-gl-native: java.lang.NullPointerException: Attempt to invoke virtual method 'android.graphics.Bitmap$Config android.graphics.Bitmap.getConfig()' on a null object reference

Created on 20 Jul 2019  路  2Comments  路  Source: mapbox/mapbox-gl-native


I don't quite get this still, anyone can help out?

Here's my code:

public Bitmap getBitmap() {
        Bitmap mBitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.ic_marker_orange);

        if (mBitmap != null && mBitmap.getConfig() != Bitmap.Config.ARGB_8888) {
            mBitmap = mBitmap.copy(Bitmap.Config.ARGB_8888, false);
        }
        return mBitmap;
    }

    private void setUpImage(@NonNull Style loadedMapStyle) {
        loadedMapStyle.addImage(MARKER_IMAGE_ID, getBitmap());
    }

Error:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.graphics.Bitmap$Config android.graphics.Bitmap.getConfig()' on a null object reference
        at com.mapbox.mapboxsdk.maps.Style.toImage(Style.java:1026)
        at com.mapbox.mapboxsdk.maps.Style.addImage(Style.java:341)
        at com.mapbox.mapboxsdk.maps.Style.addImage(Style.java:315)
        at com.android.looc.MapFragment.setUpImage(MapFragment.java:167)
        at com.android.looc.MapFragment.access$400(MapFragment.java:54)
        at com.android.looc.MapFragment$1.onStyleLoaded(MapFragment.java:107)
        at com.mapbox.mapboxsdk.maps.MapboxMap.notifyStyleLoaded(MapboxMap.java:855)
        at com.mapbox.mapboxsdk.maps.MapboxMap.onFinishLoadingStyle(MapboxMap.java:212)
        at com.mapbox.mapboxsdk.maps.MapView$MapCallback.onDidFinishLoadingStyle(MapView.java:1264)
        at com.mapbox.mapboxsdk.maps.MapChangeReceiver.onDidFinishLoadingStyle(MapChangeReceiver.java:198)
        at com.mapbox.mapboxsdk.maps.NativeMapView.onDidFinishLoadingStyle(NativeMapView.java:1035)
        at android.os.MessageQueue.nativePollOnce(Native Method)
        at android.os.MessageQueue.next(MessageQueue.java:148)
        at android.os.Looper.loop(Looper.java:151)
        at android.app.ActivityThread.main(ActivityThread.java:5673)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:969)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:764)
Android

Most helpful comment

@Noxagon thank you for reaching out and using our products. Can you confirm that R.drawable.ic_marker_orange is a vector drawable? decodeResource will only work for bitmap drawable.

I was able to reproduce the same NullPointerException using a vector drawable. Using following code mitigates the crash:

    Drawable drawable = ResourcesCompat.getDrawable(getResources(), R.drawable.ic_my_location, null);
    Bitmap mBitmap = BitmapUtils.getBitmapFromDrawable(drawable);

All 2 comments

@Noxagon thank you for reaching out and using our products. Can you confirm that R.drawable.ic_marker_orange is a vector drawable? decodeResource will only work for bitmap drawable.

I was able to reproduce the same NullPointerException using a vector drawable. Using following code mitigates the crash:

    Drawable drawable = ResourcesCompat.getDrawable(getResources(), R.drawable.ic_my_location, null);
    Bitmap mBitmap = BitmapUtils.getBitmapFromDrawable(drawable);

great to hear @Noxagon, closing as resolved

Was this page helpful?
0 / 5 - 0 ratings