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
VipulPathak
Posts: 81
Registered: ‎07-16-2008
My Device: Curve - 8320

Class Cast Exception after passing Object using ApplicationManager.postGlobalEvent()

Hello Everyone,

 

I have two application and a need to communicate between them. I am using GlobalEventListener and ApplicationManager.postGlobalEvent() method to achieve this.

 

I have a class 'ValueContainer' in the posting application (app2) and the posting application is passing this object to ApplicationManager.postGlobalEvent().

 

My question is to know, how to use this object in the receiving application (app1), given the class 'com.company.app2.ValueContainer' is a part of app2. In eventOccured callback method, object1.toString() shows that it is a ValueContainer.

 

I tried to copy the ValueContainer class in app1 also, but still a ClassCastException when casting object1 as a ValueContainer.

 

On the other hand, when I pass a string in object1, it is casted correctly to a string.

 

Anything that you would recommend to do, in order to successfully pass an application specific object to another application ?

 

Every help is greatly appreciated.

 

Thanks,

 

*(Vipul)() ;
Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: Class Cast Exception after passing Object using ApplicationManager.postGlobalEvent()

The Object that is passed must be known and the same in both Applications. 

 

A String will work, because it uses a shared RIM supplied Object. 

 

As you have found out, it is difficult to get two Application classes the same. 

"I tried to copy the ValueContainer class in app1 also, but still a ClassCastException when casting object1 as a ValueContainer."

The package name is different, even though the two classes are called ValueContainer.

 

The standard way around this is to have a shared Library that both the Applications use.  However this then causes problems wit distribution. 

 

So in reality, you have three choices:

a) Use an none Application specific collection - like a Hashtable or Vector

b) Serialize the Object to some common format that enables you to recreate it - for example convert your Object to XML in your sending and parse the XML in the receiving. 

c) Have a shared Library that contains the definition of the shared Object

Developer
VipulPathak
Posts: 81
Registered: ‎07-16-2008
My Device: Curve - 8320

Re: Class Cast Exception after passing Object using ApplicationManager.postGlobalEvent()

Thanks Peter,

 

A few additional questions:

 

>>>  The package name is different, even though the two classes are called ValueContainer.

[Vipul]  I copied the class with its entire packet structure. So ValueContainer is called com.company.product.ValueContainer in both the applications.

 

 

I created a Library and made com.company.product.ValueContainer a part of that Library. Then I made app1 and app2 use that Library. Both the applications compiled fine and installed on the device. However, the application that receives ValueContainer failed with same error at the same place, where it was failing earlier, ClassCastException.

 

When I do an object1.toString() it tells me that the object is of type:  com.company.product.ValueContainer@d9009a30. Yet type casting fails.

 

 

>>> c) Have a shared Library that contains the definition of the shared Object

[Vipul]  How to make a Library Shared among different applications ?

 

Can you throw some light on it ?

 

Thanks,

 

(*Vipul)() ;

 

*(Vipul)() ;
Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: Class Cast Exception after passing Object using ApplicationManager.postGlobalEvent()

AFAIK, and this topic has been discussed before at length, so search round, having multiple classes with exactly the same name is not a good thing on a BlackBerry and may not work the way you expect.  So I'm not sure if this approach will work but would not spend any time researching it.

 

However your Library option should have worked.  I certainly use this technique in other places.  So I'm not sure why this approach did not work.  So that I (or someone else) can have a go at replicating this, can you give OS levels involved and so on. 

 

Can you just confirm that there was only one library, not two identical libraries.  In the JDE I would have a Workspace with one Application, a WorkSpace with another Application and a separate Workspace with the Library.  How did you do it?

 

Re setting up a shared Library, I do it in the JDE.  I don't know how to do it in Eclipse.  Do you use JDE or Eclipse?

 

Developer
VipulPathak
Posts: 81
Registered: ‎07-16-2008
My Device: Curve - 8320

Re: Class Cast Exception after passing Object using ApplicationManager.postGlobalEvent()

>>> Can you just confirm that there was only one library, not two identical libraries. 

 

[Vipul]  I am using ANT script to build the projects, I am Building each project (app1 and app2) with the Library's JAR file. Does that install the Library twice and it is no more shared ?

 

 

 

>>>  In the JDE I would have a Workspace with one Application, a WorkSpace with another Application and a separate Workspace with the Library.  How did you do it?

 

[Vipul]  Does that mean you build 3 binaries and install them separately ?   After installation, does the Library appear separately in the installed modules list ?

 

 

>>> Re setting up a shared Library, I do it in the JDE.  I don't know how to do it in Eclipse.  Do you use JDE or Eclipse?

 

[Vipul]  I am using ANT script, but you can tell me the way you are doing. I will try to map it with the build script.

 

 

System Details: Blackberry 8320 Curve, Platform Version: 2.7.0.43,  Software Version: 4.5.0.18

 

Thanks for your help.

 

*(Vipul)() ;
Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: Class Cast Exception after passing Object using ApplicationManager.postGlobalEvent()

"Does that mean you build 3 binaries and install them separately ?   After installation, does the Library appear separately in the installed modules list ?"

 

Correct, three separate builds.

 

You need to do the Library first, and make sure that the jar for the Library is available to the builds for the two applications.

Developer
VipulPathak
Posts: 81
Registered: ‎07-16-2008
My Device: Curve - 8320

Re: Class Cast Exception after passing Object using ApplicationManager.postGlobalEvent()

OK, I tried as you suggested. Here is what I got:

 

I built and installed the Library as a separate COD file. Now I can see the library in the installed applications list.

 

The I built both the application separately:

 

1. Using the Library's JAR file (I passed the JAR file name to rapc). I used the target specified below:


  <target name="rapc-ipc-app" depends="package" >
    <echo message="rapc-app: main.class=${main.class}"/>
    <exec dir="${tojar.dir}" executable="${build.jars}/rapc.exe">
      <arg line=" import=${net_rim_api.jar} "/>
      <arg line=" codename=${codename} "/>
      <arg line=" class=${main.class} "/>
      <arg line=" ${jadfile} "/>
      <arg line=" ${jarfile} "/>
      <arg line=" ${ipcLibClassPath} "/>
    </exec>
  </target>

In this case when, I generate my application's COD by specifying the Library's JAR, the behaviour is same as earlier (i.e. The object is passed and is available in eventOccured callback, but attempt to typecast is causes a ClassCastException)  :-)

 

 

2.  Without using the Library's JAR file (I dropped the JAR file name in rapc command). I used the target specified below:


  <target name="rapc-ipc-app" depends="package" >
    <echo message="rapc-app: main.class=${main.class}"/>
    <exec dir="${tojar.dir}" executable="${build.jars}/rapc.exe">
      <arg line=" import=${net_rim_api.jar} "/>
      <arg line=" codename=${codename} "/>
      <arg line=" class=${main.class} "/>
      <arg line=" ${jadfile} "/>
      <arg line=" ${jarfile} "/>
    </exec>
  </target>

Please note that the last argument, (the IPC library JAR is dropped in this case). In this case when, I generate my application's COD by specifying the Library's JAR, then either of the Application on the device doesn't starts. It says- No Entry Point Defined for the Application "<Application Name>".

 

 

 

*(Vipul)() ;
Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: Class Cast Exception after passing Object using ApplicationManager.postGlobalEvent()

[ Edited ]

Apologies, I do not understand how you have built this.  I don't directly run rapc and I use JDE not Eclipse. 

 

All I can say is that my understanding suggests that a shared Library cod file will work and the practice, when this is built using JDE, is that it does.  I'm not capable of looking at your build process or practices to determine if this is an issue in your Build, or a fluke that it works for me. 

 

Sorry.

Developer
VipulPathak
Posts: 81
Registered: ‎07-16-2008
My Device: Curve - 8320

Re: Class Cast Exception after passing Object using ApplicationManager.postGlobalEvent()

Nevermind Peter,

 

Thanks for your time and suggestions  :-)

 

 

*(Vipul)() ;