5 Replies Latest reply on Apr 10, 2008 12:42 AM by Devis Balsemin

    Servlet and EJB3  nullPointerException

    Devis Balsemin Newbie

      Hi,
      i'm new here and with Jboss Seam hi to all.
      I'm using jboss 2.1.0.A1 with Flex 3 and all work fine, but i have develope a simple servlet that runs inside my jboss seam application and call my UserAction ejb.
      but i receive always a NullPointerException.
      Pls can you help me how i can use my UserAction inside a Servlet?
      Sorry if is it a studip question, but it's 3 days that i'm trying to resolve this.


      I have also used


      EntityManagerFactory factory = (EntityManagerFactory) Naming.getInitialContext().lookup("java:/FlexThorEntityManagerFactory");
      em = factory.createEntityManager();
      



      my findByCode funcition works fine.. but merge function don't throw anything but don't save anything.


      Pls guys have you some suggestions?
      Thank in advance
      Devis




      @Stateful
      @Startup
      @Name("useraction")
      //@Restrict("#{identity.loggedIn}")
      @Scope(ScopeType.APPLICATION)
      public class UserAction implements IUser{
         
          @In
          private EntityManager em;
      @Logger 
         private Log log;
      
         
         
         @In
         private FacesMessages facesMessages;
        
         
         
          public UserAction(){
             
                  System.out.println("Create ");
      //            EntityManagerFactory factory = (EntityManagerFactory) Naming.getInitialContext().lookup("java:/FlexThorEntityManagerFactory");
        //          em = factory.createEntityManager();
             
          }
          
          
          public CUser merge(CUser vo) {
              vo=em.merge(vo);
              System.out.println("-------------- Salvato "+vo.getCdcusr()+" "+vo.getDscusr()+" "+vo.getSteusr());
              
              return vo;
          }
      
          public void delete(CUser vo) {
           if((vo=findByCode(vo.getCdcusr()))!=null)
               em.remove(vo);
          }
      
          public ArrayList<CUser> findAll() {
            return (ArrayList<CUser>) em.createQuery("select h from CUser h ").getResultList();
          }
      
          public CUser findByCode(String _code) 
          {
          try{
              CUser us=(CUser) em.createQuery("select h from CUser h where h._cdcusr='devis' ").getResultList().get(0);
              System.out.println("-------------- "+us.getDscusr());
              return us;
             }
          catch(Exception e){
              e.printStackTrace();
              return null;
          }
          }
      
          public ArrayList<CUser> findBy(String _txt) {
              throw new UnsupportedOperationException("Not supported yet.");
          }
      
          @Destroy @Remove
          public void destroy() {}
      
      }
      
      



      My web.xml



        <!-- PBX Server -->
          <servlet>
              <servlet-name>FlexPbx</servlet-name>
              <servlet-class>org.pbx.StartPbx</servlet-class>
              <load-on-startup>3</load-on-startup>
          </servlet>
      
         <servlet-mapping>
              <servlet-name>FlexPbx</servlet-name>
              <url-pattern>/servlet</url-pattern>
          </servlet-mapping>
      





      My Component.xml




      <web:context-filter url-pattern="/servlet/*" />
      





      My Servlet




      
      public class StartPbx extends HttpServlet {
      
          
          public void init() throws ServletException {
              try {
                  String initial = getInitParameter("server_pbx");
                  System.out.println("????????????? "+initial);
                  /* try {
                  count = Integer.parseInt(initial);
                  }
                  catch (NumberFormatException e) {
                  count = 0;
                  }*/
                  UserAction action =new UserAction();
                  CUser vo =action.findByCode("devis");
                  //PbxTrace trace = new PbxTrace();
                  //trace.run();
              } catch (Exception ex) {
                  ex.printStackTrace();
              }
        }
      
      

        • 1. Re: Servlet and EJB3  nullPointerException
          Nicklas Karlsson Master

          does em.joinTransaction() and/or em.flush() do anything?

          • 2. Re: Servlet and EJB3  nullPointerException
            Devis Balsemin Newbie

            Thank Nicklas, later i will try it your suggest,
            but i don't understand if to use my Seam UserAction from my Servlet   i must use


            EntityManagerFactory factory = (EntityManagerFactory) Naming.getInitialContext().lookup("java:/FlexThorEntityManagerFactory");
            em = factory.createEntityManager();
            



            thank you very much for your help
            Devis

            • 3. Re: Servlet and EJB3  nullPointerException
              Nicklas Karlsson Master

              Try dropping web:context-filter and wrapping your servlet in a


              
              protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
              
                new ContextualHttpServletRequest(request) {
              
                  public void process() throws Exception {
              
                    // Access seam stuff with Component.getInstance(...)
              
                  }
              
                }.run();
              
              }
              
              

              • 4. Re: Servlet and EJB3  nullPointerException
                Devis Balsemin Newbie

                Thank Nicklas thank you very much.. It's work fine.
                Devis

                • 5. Re: Servlet and EJB3  nullPointerException
                  Devis Balsemin Newbie

                  Sorry... i don't understand because now receive this error.
                  Pls have you some suggestion thank you in advance
                  Devis



                  javax.ejb.NoSuchEJBException: Could not find stateful bean: 5c4o13-jgbvsm-feugnl68-1-feugtwtq-x
                       at org.jboss.ejb3.cache.simple.SimpleStatefulCache.get(SimpleStatefulCache.java:390)
                       at org.jboss.ejb3.cache.simple.SimpleStatefulCache.get(SimpleStatefulCache.java:375)
                       at org.jboss.ejb3.stateful.StatefulInstanceInterceptor.invoke(StatefulInstanceInterceptor.java:61)
                       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
                       at org.jboss.aspects.security.AuthenticationInterceptor.invoke(AuthenticationInterceptor.java:77)
                       at org.jboss.ejb3.security.Ejb3AuthenticationInterceptor.invoke(Ejb3AuthenticationInterceptor.java:110)
                       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
                       at org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.java:46)
                       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
                       at org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(AsynchronousInterceptor.java:106)
                       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
                       at org.jboss.ejb3.stateful.StatefulContainer.localInvoke(StatefulContainer.java:206)
                       at org.jboss.ejb3.stateful.StatefulLocalProxy.invoke(StatefulLocalProxy.java:119)
                       at $Proxy200.findByCode(Unknown Source)
                       at sun.reflect.GeneratedMethodAccessor182.invoke(Unknown Source)
                       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
                       at java.lang.reflect.Method.invoke(Method.java:597)
                       at org.jboss.seam.util.Reflections.invoke(Reflections.java:21)
                       at org.jboss.seam.intercept.RootInvocationContext.proceed(RootInvocationContext.java:31)
                       at org.jboss.seam.intercept.ClientSideInterceptor$1.proceed(ClientSideInterceptor.java:76)
                       at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:56)
                       at org.jboss.seam.ejb.RemoveInterceptor.aroundInvoke(RemoveInterceptor.java:41)
                       at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
                       at org.jboss.seam.intercept.RootInterceptor.invoke(RootInterceptor.java:118)
                       at org.jboss.seam.intercept.ClientSideInterceptor.invoke(ClientSideInterceptor.java:54)
                       at org.javassist.tmp.java.lang.Object_$$_javassist_0.findByCode(Object_$$_javassist_0.java)
                       at org.pbx.PbxTrace.changeState(PbxTrace.java:150)
                       at org.pbx.PbxTrace.propertyChange(PbxTrace.java:99)
                       at java.beans.PropertyChangeSupport.firePropertyChange(PropertyChangeSupport.java:339)
                       at java.beans.PropertyChangeSupport.firePropertyChange(PropertyChangeSupport.java:276)
                       at org.asteriskjava.live.internal.AbstractLiveObject.firePropertyChange(AbstractLiveObject.java:76)
                       at org.asteriskjava.live.internal.AsteriskChannelImpl.setCallerId(AsteriskChannelImpl.java:211)
                       at org.asteriskjava.live.internal.ChannelManager.handleNewCallerIdEvent(ChannelManager.java:447)
                       at org.asteriskjava.live.internal.AsteriskServerImpl.onManagerEvent(AsteriskServerImpl.java:850)
                       at org.asteriskjava.manager.ManagerEventListenerProxy$1.run(ManagerEventListenerProxy.java:77)
                       at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:885)
                       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:907)
                       at java.lang.Thread.run(Thread.java:619)
                  




                  MyServlet


                    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                        System.out.println("------ doGet()-----------------");
                    new ContextualHttpServletRequest(request) {
                      public void process() throws Exception {
                              
                              IUser action =(IUser) Component.getInstance("useraction");
                              PbxTrace trace = new PbxTrace(action,ip_pbx,user_pbx,pwd_pbx);
                              trace.run();
                        
                      }
                    }.run();
                  }
                  



                  MyClass


                  @Stateful
                  @Name("pbxtrace")
                  //@Restrict("#{identity.loggedIn}")
                  @Scope(ScopeType.APPLICATION)
                  public class PbxTrace implements IPBX, AsteriskServerListener, PropertyChangeListener {
                  
                      PbxTrace(IUser action, String ip_pbx, String user_pbx, String pwd_pbx) {
                          this.user = action;
                  
                      }
                  
                  
                          CUser tmp = user.findByCode(callerid);
                          if (tmp != null) {
                              tmp.setSteusr(state);
                              user.Merge(tmp);
                          }