Karry
Karry

Reputation: 153

NullPointer on Bitmap.getWidth in Codenameone Android app

I have an app created in CodenameOne running on Android that is throwing NullPinterExpections after trying to scale too many images. I've used CacheMap to help the situation but still get the issue after loading too many images.

Here is my code that throws the issue, from within InfiniteContainer fetchComponents

Image i = (Image) MoveService.getInstance().getImage(thumbnail_url);
        if (i == null) {
            i = theme.getImage(move.getThumbnail_url());
            if (i != null) {
                i = i.fill(width+20, (width / 2) * 3);
                MoveService.getInstance().putImage(move.getThumbnail_url(), i);
            }
        }

And here is what I get from the logs:

[EDT] 0:8:57,596 - Exception: java.lang.NullPointerException - Attempt to      invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference
java.lang.NullPointerException: Attempt to invoke virtual method 'int  android.graphics.Bitmap.getWidth()' on a null object reference
    at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:748)
    at com.codename1.impl.android.c.a(AndroidImplementation.java:1688)
    at com.codename1.k.s.c(Image.java:1008)
    at com.codename1.k.s.c_(Image.java:954)
    at com.codename1.k.s.b(Image.java:919)
    at com.codename1.impl.android.c$8.a(AndroidImplementation.java:6216)
    at com.codename1.k.n.a(EncodedImage.java:627)
    at com.codename1.k.n.b(EncodedImage.java:654)
    at com.codename1.k.s.e(Image.java:903)
    at com.codename1.k.s.f(Image.java:974)
    at com.altitude.studios.polebible.e$b.<init>(Unknown Source)
    at com.altitude.studios.polebible.e$16.a(Unknown Source)
    at com.codename1.k.u.m(InfiniteContainer.java:143)
    at com.codename1.k.u$5.run(InfiniteContainer.java:172)
    at com.codename1.k.m.l(Display.java:1154)
    at com.codename1.k.m.j(Display.java:1098)
    at com.codename1.k.m.i(Display.java:999)
    at com.codename1.k.ad.run(RunnableWrapper.java:120)
    at com.codename1.impl.b$1.run(CodenameOneThread.java:60)
    at java.lang.Thread.run(Thread.java:818)

Upvotes: 1

Views: 162

Answers (1)

Shai Almog
Shai Almog

Reputation: 52760

This can happen if an image went thru the wrong pipeline and the underlying native image is gone. If the image is a font image or a similar "exotic" image try converting it to a regular image using toEncodedImage() or similar method.

It can also happen if you explicitly invoked dispose() on an image.

Upvotes: 0

Related Questions