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
Developer
rock_win
Posts: 136
Registered: ‎02-01-2009
My Device: Not Specified

Static reference returning different values every time

Hi,

 

I’m developing an application to log call data on Blackberry Bold -9000. The application works as both ‘foreground’ and ‘background’ application.

 

The call data is being stored in a ‘static Vector’.

 

But when I try to check the data stored in the ‘Vector’, it returns different set of values in both states.

 

I also tried checking the ‘hashCode ()’ value of the reference and they were also different between the two states.

 

Please let me know any other setting to correct this issue.

---------------------------------------------------------------------------------------------------------------------

 

My code uses the following implementation on JDE 4.7, Blackberry Bold-9000:

 

---Application Main file---

PhoneLogs.addListener(new Log1());

 

---Screen File ---

extends MainScreen

public static Vector vector=new Vector();

protected void paint(Graphics graphics) {      

    System.out.println(vector);

    super.paint(graphics);

    }

 

-----Log file ----

implements PhoneLogListener

public void callLogAdded(CallLog cl) {

        try {

        Screen1.vector.addElement(cl);

        System.out.println(Screen1.vector);

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

 

 

Developer
knight9
Posts: 84
Registered: ‎09-21-2008
My Device: Not Specified

Re: Static reference returning different values every time

static variables can be confusing on the BlackBerry... Without going into a long discussion, the solution is to instead store your vector in the RuntimeStore instead of as a static variable. If you use the runtime store to reference your vector from both entry points then you will get the results you expect. 

 

Just look up the java doc for Runtime store. 

Developer
knight9
Posts: 84
Registered: ‎09-21-2008
My Device: Not Specified

Re: Static reference returning different values every time

[ Edited ]

Some sample code to use a vector with runtime store. whenever you want to use the vector just call getVector() instead, this should get you back up and running. Note the MY_VECTOR_ID should be some unique value, you can use the "covert to long" functionality in the JDE to generate a unique value from a string. 

 

private static final long MY_VECTOR_ID = 0x980ccccb59344017L

public static Vector getVector(){

Vector _vector;

RuntimeStore rs = RuntimeStore.getRuntimeStore();

synchronized( rs ){

_vector = (Vector) rs.get( MY_VECTOR_ID );

if( _vector == null ){

_vector = new Vector();

rs.put( MY_VECTOR_ID, _vector );

}

}

return _vector;

}

 


 

Editing because the board messed up the formatting

Message Edited by knight9 on 09-19-2009 01:17 AM
Developer
Kenjkelly01
Posts: 64
Registered: ‎01-12-2009
My Device: Blackberry Storm, Curve, Tour

Re: Static reference returning different values every time

I had a similar problem accessing a static queue in a singleton thread from a callback.  I would guess you are having the problem from the callback (PhoneListener) vs accessing the variable from elsewhere in your app.  I used a static method to access the static queue and that seemed to solve the issue.  If that hadn't worked I was going to use the runtimestore.

 

  I am interested in the comment above "On the Blackberry"  because I was wondering if this was just J2ME related or a typical Java problem. I never ran into the issue on J2SE or Android.

 

 

Ken J Kelly
Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: Static reference returning different values every time

I get the impression that this is an issue that is specific to the BlackBerry JVM.  I agree this is not a problem with most PC based VMs, sorry can't comment on Android. 

 

My understanding of the 'problem' goes like this.  When the BlackBerry VM starts a process it allocates the static variables.  So every process gets its own static variables.  This means that the Phone application and the email application and your application have their own static variables.  So when the Phone application or the email application invoke your listener, the static variables will be allocated again and will bear no relation to the ones that you see in your Ui Application. 

 

It gets even more confusing when you have an alternate entry point application, which starts both a background process and a UI process.  These are two different processes and so the static variables in the background task are not the same as the one for the UI. 

 

As has already been pointed out, this is not a big issue, RuntimeStore provides a good solution to this. 

 

Just remember a static is NOT a Singleton, except in a single process application. 

Developer
Posts: 1,474
Registered: ‎04-14-2009
My Device: Not Specified

Re: Static reference returning different values every time

From what I know, in a nutshell, every time a BlackBerry Java application starts, a new ClassLoader is created for loading the classes it needs. As a result, two different applications/processes (e.g., Phone application, your application started via an icon, you application started via a system boot entry point, your application started via an ApplicationDescriptor) will see different instances of the "same" class if they access the class via Class.forName() or if they access static fields/members of the class via compiled code (e.g., code like Screen1.vector). This also means that the static field references of the "same" class as seen by different processes may be different.

 

On BlackBerry the Singleton pattern cannot be properly implemented only using a static field of a class. The RuntimeStore is a relatively cheap and straightforward way of implementing a proper singleton. Nore that you can still use a static-field-only-based singletons if you are sure they won't be shared between processes/applications. The most common scenario where this "sharing" occurs inadvertently is where you implement a callback/listener in your code and then this callback is invoked from another application (e.g., Phone application). Another typical scenario is where your application does something on system startup (e.g., registers some listeners) and then terminates -- next time your application starts it will be a different process.

Developer
ronenfe
Posts: 77
Registered: ‎06-03-2009
My Device: Not Specified

Re: Static reference returning different values every time

[ Edited ]

I've been trying for hours to understand why my static vector is different when I'm in my main class and when I'm in the callloglistener class, that seems to be the problem.

 

I will try to use  the runtime store.

 

edit, I'm using the code posted above.

 

All the strange problems i had which i couldn't explain are gone, and everything works faster, even the simulator.

 

My conclusion is never to use a static vector which is accessed by the alternate entry point.

 

Because it will make it duplicate and slow everything, and  cause unpredictable behavior.

Developer
RexDoug
Posts: 4,764
Registered: ‎07-21-2008
My Device: Not Specified

Re: Static reference returning different values every time

It all leads back to the way that the JVM marshals threads. The listener thread belongs to the calling process (call log, in your case).