views:

1343

answers:

3

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.

A: 

What portion is null? eve.result should be your returned object. eve.message is for messaging services I believe.

CookieOfFortune
eve.message.body.toString() is printing out the message from my function, it's printing out "Hiiii" if I don't have create(categories.get(i)) in my program.
Maksim
+2  A: 

Are you using the ejb3 factory?

James Ward
Yeah... this works, but now I can only access Facades, no controllers.Thanks.
Maksim
+1  A: 

@EJB only works (in a default, generic JEE container) with managed objects, notably Servlets and other Session Beans.

Out of the box, @EJB doesn't work with any ol' class.

Flex would have to have specific support for the @EJB notation for this too work.

Will Hartung
It will actually work in this context because the request is coming through the MessageBroker Servlet. I have a project using the ejb3 factory for remoting that is doing exactly this.
James Ward