• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Got hooked up with nullpointerexception

B8787

Newbie
I am getting a nullpointerexception. Only thing is, it doesn't appear to be there at all devices, so is rather strange to me.. anyway, does anybody know what is causing this, an idea is that it would be Bitmap, but since I declare it myself I guess this is not correct? Since it is not happening on my own device I just have to be sure...

[HIGH]Bitmap bm = BitmapFactory.decodeResource(C.getResources(), resId);
Bitmap result = mark(bm);

public static Bitmap mark(Bitmap src) {
int w = src.getWidth();
// int w = 150;
int h = src.getHeight();
// int h = 150;
Bitmap result = Bitmap.createBitmap(w, h, src.getConfig());

Canvas canvas = new Canvas(result);
canvas.drawBitmap(src, 0, 0, null);

Paint paint = new Paint();
paint.setColor(Color.GRAY);
paint.setTypeface(font);
paint.setTextSize(15);
paint.setAntiAlias(true);
canvas.drawText("Hi", 10, 15, paint);

return result;
}[/HIGH]
 
BitmapFactory can fail to load images for various reasons, in this case the bitmap will be null. Usually it will print some messages to logcat to explain what went wrong (may be out of memory).

Some devices running newer versions of android will also fail to load large bitmaps because they are loaded into GPU surfaces. GPU surfaces often have a maximum width and height of 1024 or 2048 pixels.
 
BitmapFactory can fail to load images for various reasons, in this case the bitmap will be null. Usually it will print some messages to logcat to explain what went wrong (may be out of memory).

Some devices running newer versions of android will also fail to load large bitmaps because they are loaded into GPU surfaces. GPU surfaces often have a maximum width and height of 1024 or 2048 pixels.

It happens on devices with Android 1.5 and 2.1-update 1. Is there a way to catch this error? Or to at least go around it and that these devices get a message like 'Not supported on this Android version' or something?

Grts
 
You can test for null with if(bm == null) but you should probably try and understand what is going wrong. Null pointer exceptions are generally quite easy to debug since you get good information in the logcat output. It a should tell you exactly which line of code the error occurred on and if it is an error decoding the bitmap you will get detailed information about the problem.
 
You can test for null with if(bm == null) but you should probably try and understand what is going wrong. Null pointer exceptions are generally quite easy to debug since you get good information in the logcat output. It a should tell you exactly which line of code the error occurred on and if it is an error decoding the bitmap you will get detailed information about the problem.

But is this information easy to get if it is not happening on my device? It seems like it is limited to 3 android versions (1.5, 1.6 and 2.1-update1)..
 
You can try and reproduce the problem on the Emulator or see if anyone you know has an affected device which you can borrow. If your app is published on the play store you can sometimes get crash reports with logs through the developer console. But it relies on people actually submitting crash info, and I don't think it is supported on Android 1.5.
 
I tried the Emulator (for some reason I forgot about that, I always test on my mobile phone).
It crashes with Android 1.5 and 1.6 on the emulator.
Got some more info:

02-25 22:39:10.382: E/AndroidRuntime(315): at android.graphics.Bitmap.createBitmap(Bitmap.java:468)
02-25 22:39:10.382: E/AndroidRuntime(315): at com.eyeballinteractive.logoquiz.Answer.mark(Answer.java:439)
02-25 22:39:10.382: E/AndroidRuntime(315): at com.eyeballinteractive.logoquiz.Answer.onClick(Answer.java:395)


Line 439:
Bitmap result = Bitmap.createBitmap(w, h, src.getConfig());

Line 395:
Bitmap result = mark(bm);
 
You are probably running out of memory, do you create a lot of bitmaps?
When you are finished using a bitmap object you should call recycle() on it to free its memory, bitmap data isn't garbage collected very frequently.
 
It is the only bitmap which I create... Didn't get a outofmemoryerror also.. I really think it depends on the Android version..

Now added this:

Bitmap bm = BitmapFactory.decodeResource(getApplicationContext().getResources(), resId);
if (bm != null) {
res = mark(bm);
//rest of the code
} else { Toast..... }

But keep getting the same error? Wtf, I lost it here ..
 
Its not the call to decodeBitmap() that is returning null, the null pointer exception is actually happening inside the call to createBitmap(), probably because it couldn't allocate some memory and it got null instead.

How many bitmaps do you have and how big are they? And do you call mark on all of them?

If you are calling mark() frequently then you may be leaking bitmaps.
 
Well it actually happens when you want to send an image to a friend... and that doesn't happen automatically, so just that one image of 150x150px... they differ in size but never bigger than 15kb.
 
Solution for now: I surrounded the functions with try and catch.. if it catches an error it displays a toast...

Sounds ok, right?
 
Back
Top Bottom