1

I got a java.lang.NullPointerException when I try to call createBitmap. Here is my code:

board = Bitmap.createBitmap(handler.signs.length * cellSize, 
        handler.signs[0].length * cellSize, Bitmap.Config.ARGB_8888);

where the width and height are also greater than zero. And this is the stack trace:

java.lang.NullPointerException
at android.graphics.Bitmap.createBitmap(Bitmap.java:478)
at org.me.five_stones_project.game.GameView.drawBoard(GameView.java:334)
at org.me.five_stones_project.game.GameView.increaseBoard(GameView.java:293)
at org.me.five_stones_project.game.GameHandler.checkStatus(GameHandler.java:304)
at org.me.five_stones_project.game.GameHandler.makeMyStep(GameHandler.java:211)
at org.me.five_stones_project.game.GameView.onTouchEvent(GameView.java:238)
at android.view.View.dispatchTouchEvent(View.java:3891)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1719)
at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1150)
at android.app.Activity.dispatchTouchEvent(Activity.java:2102)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1703)
at android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2200)
at android.view.ViewRoot.handleMessage(ViewRoot.java:1884)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:861)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:619)
at dalvik.system.NativeStart.main(Native Method)

I took a look at the source code of the Bitmap and I found this:

476         public static Bitmap createBitmap(int width, int height, Config config) {
477         Bitmap bm = nativeCreate(null, 0, width, width, height, config.nativeInt, true);
478         bm.eraseColor(0);    // start with black/transparent pixels
479         return bm;
480     }

So the exception occurs when bm.eraseColor(0) is called?

Can somebody help me? Thanks!

tungi52
  • 632
  • 1
  • 8
  • 15
  • 1
    Does this happen with fixed sized, say 16x16 for example, Bitmap creation too? – harism Jan 17 '12 at 23:25
  • Most likely `nativeCreate()` returns null, and that's why it fails at `bm.eraseColor()`. Try it with fixed values for width and height as @harism said so that you can isolate the error. It might also be worthwhile to try it with a different Bitmap.Config value. – theisenp Jan 18 '12 at 00:51

2 Answers2

5

I traced several intermittent errors where Bitmap.createBitmap() returned null; they happened mostly in Chinese Android phones. The parameters being passed were ok, but I found that the jvm was very low on memory whenever this occurs.

My theory is there are cases where createBitmap() runs out of memory and returns null instead of throwing OutOfMemoryException (which is what it usually does).

My "fix" is to check if createBitmap() returns null and throw our own OutOfMemoryException so it is correctly categorized. Then our error logging treats it as a device issue rather than a functional bug.

mwk
  • 1,959
  • 1
  • 14
  • 22
  • mwk +1 Very well said .Is there any solution of this prob – maveric Mar 12 '15 at 05:49
  • 1
    That could work if createBitmap returns null, but this issue is createBitmap throws a NullPointerException, and probably has a completely different cause. – nasch Aug 20 '15 at 18:25
1

Try use createScaledBitmap instead, I face the same problem before, the reason i remember is the width or the height you want for the bitmap exceeds the size of the device.

Hope this will help you.

dreamtale
  • 2,905
  • 1
  • 26
  • 38
  • Sorry, but I don't really understand how this can help me. In my app I create an empty bitmap first, then I fill with content. But createScaledBitmap needs a Bitmap as first parameter then a scale factor (new width and height). – tungi52 Jan 18 '12 at 09:37
  • Or you can use `Bitmap.createBitmap`, but you must guarantee the width and the height you put in doesn't exceeds the size of the device. – dreamtale Jan 18 '12 at 10:29