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

Apps Null object reference, but why?

Im trying to costumize some code from a book to fit my own purpose, however im getting this error
" Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference" in the int resID line, but im not sure why. If someone can point me in the right direction it will be greatly appreciated!

Code:
public Bitmap prepareBitmap(Context _context, String _bitmapName, int _pixelsPerMetre)
{

    // Make a resource id from a String that is the same name as the .png
    int resID = _context.getResources().getIdentifier(_bitmapName,"drawable", _context.getPackageName());

    // Create the bitmap
    Bitmap bitmap = BitmapFactory.decodeResource(_context.getResources(), resID);

    // Scale the bitmapSheet based on the number of pixels per metre
    // Multiply by the number of frames contained in the image file
    // Default 1 frame
    bitmap = Bitmap.createScaledBitmap(bitmap, (int) (width * animFrameCount * _pixelsPerMetre),
                                               (int) (height * _pixelsPerMetre), false);

    return bitmap;
}
 
Last edited by a moderator:
To understand this, we need to know where in the code you call method prepareBitmap(). The error is happening because parameter '_context' is null. Whatever line of code has called this method, has supplied a null value.
 
Thanks, because of your answer i found the bugger! It was a Context variable that was being passed on a couple of times, and somewhere in there the variable name was changed from context to _context, hence it didnt get passed on properly! But it works as a charm now! :D
 
Back
Top Bottom