Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
New Contributor
Posts: 7
Registered: ‎05-13-2012
My Device: Developer
My Carrier: Developer

Bitmap object as member variable is always null

Hi guys,

 

I am trying to create a Bitmap object and assign it to a member variable using the line below, but it is always null:

 

Bitmap.getBitmapResource("ca.png")

 

The line works for variables within a method but not for a member variable. Any ideas why this is happening??

 

 

Also the @Override tag doesn't is available, is this because the application uses BlackBerry's own JDK?

 

Thanks.

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Bitmap object as member variable is always null

Welcome to the forum.

 

As I understand it, Bitmap.getBitmapResource("ca.png") should always return null if the file "ca.png" is not in your resources or the Bitmap if it is.  If you are seeing different outcomes based on whereabouts in your code this is, then there is something else going wrong with your code. 

 

Also as I understand tags like @override are not supported because of the level of Java that is used, (i.e. J2ME) rather than anything else.  Sorry you just have to comment these out....

New Contributor
Posts: 7
Registered: ‎05-13-2012
My Device: Developer
My Carrier: Developer

Re: Bitmap object as member variable is always null

Regarding the @Override, thats what I have started to do.

 

 

But back to my original question, here is a screenshot of my debugger and screen. The member variable in question is "flag".

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Bitmap object as member variable is always null

[ Edited ]

Sorry it might take a while for the image to come through since it needs to be approved I think.

 

Back to my comment, if you have

 

Bitmap myBitmap = Bitmap.getBitmapResource("ca.png");

 

in two different places in your program, in both places myBitmap will be either null or not null. Is that what you see?

 

If you always see null, then make sure you have added the png to your project. 

New Contributor
Posts: 7
Registered: ‎05-13-2012
My Device: Developer
My Carrier: Developer

Re: Bitmap object as member variable is always null

[ Edited ]

I have something like this:

 

public final class MyScreen extends MainScreen
{

    Bitmap flag;

    public MyScreen()
    {

        flag = Bitmap.getBitmapResource("ca.png");
        Bitmap caFlag = Bitmap.getBitmapResource("ca.png");

}

}

 

I have included the png file as the Bitmap declared within the constructor returns/contains an object, but the member variable is null. The line " Bitmap.getBitmapResource("ca.png");" is a direct copy. 

 

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Bitmap object as member variable is always null

What is "the member variable"?

 

Looking at this code, if you put a break point immediately after

Bitmap caFlag ...

then in the debugger either

1) both "flag" and "caFlag" will be null

or

2) both "flag" and "caFlag" will not be null

 

However they will not point to the same Object, since you create a second Object with the second invocation of

Bitmap.getBitmapResource("ca.png");

 

I am sure you are already aware that this code would be more efficiently written as:

       flag = Bitmap.getBitmapResource("ca.png");
        Bitmap caFlag = flag;