0

In my drawable object class, I have a member variable that stores a bitmap which gets assigned the constructor, by the time the render function is called, the bitmap has become null and I can't work out why.

Class members and constructor:

public class MyDrawableObject {
    private int mFileLocation;
    private final int mId;
    private Context mContext;
    private Bitmap bmp;

    private int X;
    private int Y;
    private int W;
    private int H;

    public static List<MyDrawableObject> ObjectList = new ArrayList<MyDrawableObject>();

    public MyDrawableObject(int fileloc, Context con) {
        mFileLocation = fileloc;
        mId = ObjectList.size();
        mContext = con;

        ObjectList.add(this);

        Bitmap bmp = BitmapFactory.decodeResource(mContext.getResources(), mFileLocation);

        // Store width and height
        W = bmp.getWidth();
        H = bmp.getHeight();

        Log.d("DrawableObject", "Width: " + W + " Height: " + H);
        Log.d("DrawableObject", "Object Added to list, ID: " + mId);
        Log.d("DrawableObject", "ID: " + mId + " Filelocation: " + mFileLocation);
    }

Function that error occurs in

public void SetupImage(Context mContext) {
    // Create our UV coordinates.
    float[] uvs = new float[] {
            0.0f, 0.0f,
            0.0f, 1.0f,
            1.0f, 1.0f,
            1.0f, 0.0f
    };

    // The texture buffer
    ByteBuffer bb = ByteBuffer.allocateDirect(uvs.length * 4);
    bb.order(ByteOrder.nativeOrder());
    uvBuffer = bb.asFloatBuffer();
    uvBuffer.put(uvs);
    uvBuffer.position(0);

    // Generate Textures, if more needed, alter these numbers.
    int[] texturenames = new int[1];
    GLES20.glGenTextures(1, texturenames, 0);

    // Bind texture to texture name
    GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texturenames[0]);

    // Set filtering
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);

    if(bmp == null) {
        Log.d("DrawableObject", "NULL BITMAP");
    } else {
        Log.d("DrawableObject", "NON NULL");
    }

    // Load the bitmap into the bound texture.
    GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bmp, 0);
}

When I run this, I get my "NULL BITMAP" message in my logs and the app crashes. I don't modify or access bmp or in any other place other than the two examples above in the constructor and the SetupImage function above.

Any help greatly appreciated, thanks.

1 Answer 1

3

Culprit is

        Bitmap bmp =      BitmapFactory.decodeResource(mContext.getResources(), mFileLocation);

you are overriding bmp declaration, this is resulting in a local variable named bmp and will not assign to your class variable. You should change it to

        bmp = BitmapFactory.decodeResource(mContext.getResources(), mFileLocation);

Hope this helps.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much, I had a feeling it was something simple like that as I had it working before.
I saw that too. Should be a warning on this in ide.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.