Right now I'm playing with Flex and Java integration and trying to access EJB3 methods by clicking Flex button but it's giving me null pointer error.
Here is what I have in my code:
[Controller:]
public class homePageController {
@EJB
CategoryFacadeRemote categoryFacade;
public String populateDBWithXMLData(){
ArrayList<Category> cats = new ArrayList<Category>();
cats = new QuestionsUtil().getCategories();
try{
categoryFacade.createMany(cats);
}
catch(EJBException ex){
ex.printStackTrace();
}
System.out.println("SIZE: " + cats.size());
return "HIIIIII";
}
}
[CategoryFacade]
@Stateless
public class CategoryFacade implements CategoryFacadeLocal, CategoryFacadeRemote {
@PersistenceContext
private EntityManager em;
public void create(Category category) {
em.persist(category);
}
public void createMany(ArrayList<Category> categories){
for(int i = 0; i < categories.size(); i++){
create(categories.get(i));
}
}
...
}
[Remote-config.xml]
...
<destination id="homePageController">
<properties>
<source>homePageController</source>
</properties>
</destination>
...
[Flex App. File]
...
<mx:RemoteObject id="hcRO" destination="homePageController"></mx:RemoteObject>
<mx:Script>
...
private function callHomePageController():void{
hcRO.addEventListener(ResultEvent.RESULT, hcROResultHandler);
hcRO.addEventListener(FaultEvent.FAULT, hcROFaultHandler);
hcRO.populateDBWithXMLData();
}
private function hcROResultHandler(eve:ResultEvent):void{
Alert.show("RESULT:" + eve.message.body.toString());
}
private function hcROFaultHandler(eve:FaultEvent):void{
Alert.show("FAULT:" + eve.message.toString());
}
]]>
</mx:Script>
<mx:Button x="148" y="222" label="homePageController" click="callHomePageController()" width="262" height="43"/>
</mx:Application>
The program works fine if I take out the call to EJB. I'm sure that the solution is easy but I can't figure it out.
By the way, I'm using Glassfish as my app. server.