8 Replies Latest reply on Oct 28, 2004 7:05 AM by robisz

    Problem returning a string from a stateless session bean

    tomjmalone

      I have set up a stateless session bean, with should do login. I am new to ejbs,so i set it up to just reurn the username, the ejb is below.

      /**
      *
      * File: LoginHome
      *
      * Use: The login EJB
      *
      * Author: Thomas James Malone
      * Email: tom.malone@fastest.org.uk
      * Copyright: (c) Thomas James Malone
      *
      */
      // Declare the package
      package co.uk.fastest.foi.ejb.session.Login;

      // Declare the java imports

      // Declare the extention java imports
      import javax.ejb.*;
      import javax.naming.*;

      // Declare the interface
      public class LoginEJB implements SessionBean { // start of the SessionEJB class

      // Fields
      private SessionContext ctx;
      private Context enviroment;

      // Constructor
      public LoginEJB ( ) { // start of the constructor

      } // End of the consrutor

      // Methods
      public void ejbCreate ( ) throws CreateException { // start of the CreateException

      try { // start of the try statement

      InitialContext ic = new InitialContext ( );

      enviroment = ( Context ) ic.lookup( "java:comp/env" );

      } // end of the try statement

      catch ( NamingException ne ) { // start of the catch statement

      throw new CreateException ( " Could not look up the context ");

      } // end of the catch statement

      } // End of the ejbCreate

      public void ejbActivate( ) { }

      public void ejbPassivate ( ) { }

      public void ejbRemove ( ) { }

      public void setSessionContext ( SessionContext c ) { // start of the setSessionContext method

      ctx=c;

      } // End of the setSessionContext method

      public String validateUser ( String username, String password ) throws InvalidLoginException { // start of the validateUser method

      /*String outcome;

      outcome = "username";*/

      return username;

      } // end of the validateUser method

      } // end of the SessionEJB class

      the ejb-jar.xml i use is here


      <ejb-name>LoginEJB</ejb-name>
      co.uk.fastest.foi.ejb.session.Login.LoginHome
      co.uk.fastest.foi.ejb.session.Login.Login
      <ejb-class>co.uk.fastest.foi.ejb.session.Login.LoginEJB</ejb-class>
      <session-type>Stateless</session-type>
      <transaction-type>Container</transaction-type>



      And the jboss.xml file


      <ejb-name>LoginEJB</ejb-name>
      <jndi-name>foi/Login</jndi-name>



      And when the following code is called:
      try { // start of the try statement

      Object object = initialContext.lookup("foi/login");
      loginHome = ( LoginHome ) PortableRemoteObject.narrow(object, LoginHome.class);

      login = ( Login ) loginHome.create();

      status = login.validateUser(username, password);


      } // end of the try statement

      catch ( Exception e ) { // start of the catch statement

      status = e.getMessage();

      } // end of the catch exception

      Status is a String and it should be the username, but it just comes back as null, any help would be greatly received.

      Sorry for such an irratating question but i'm really stuck

      Tom

        • 1. Re: Problem returning a string from a stateless session bean
          robisz

          Probably your bean method call throws an exception and
          e.getMessage() returns with null in your catch block.

          Retry it with e.printStackTrace() in catch block.

          • 2. Re: Problem returning a string from a stateless session bean
            tomjmalone

             

            "robisz" wrote:
            Probably your bean method call throws an exception and
            e.getMessage() returns with null in your catch block.

            Retry it with e.printStackTrace() in catch block.


            your right get an exception now, why do i get an exception in the first place. Where is there an error.

            Sorry again for being so irrating


            • 3. Re: Problem returning a string from a stateless session bean
              robisz

              Maybe somebody can help, if you post the exception :-)

              • 4. Re: Problem returning a string from a stateless session bean
                tomjmalone

                sorry your right i completly forgot. This is called from a struts action class.

                java.lang.RuntimeException: java.lang.NullPointerException
                at co.uk.fastest.foi.struts.action.LoginAction.execute(LoginAction.java:72)
                at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
                at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
                at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
                at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
                at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
                at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
                at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:237)
                at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
                at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:75)
                at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:186)
                at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
                at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)
                at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
                at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
                at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:198)
                at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:152)
                at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
                at org.jboss.web.tomcat.security.CustomPrincipalValve.invoke(CustomPrincipalValve.java:66)
                at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
                at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:158)
                at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
                at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
                at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)
                at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
                at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:118)
                at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
                at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
                at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
                at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
                at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
                at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:929)
                at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160)
                at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)
                at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)
                at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)
                at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
                at java.lang.Thread.run(Thread.java:534)
                Caused by: java.lang.NullPointerException
                at co.uk.fastest.foi.struts.action.LoginAction.execute(LoginAction.java:57)
                ... 37 more

                The only thing i can think of ( and i am a complete newbie ) is that the jndi reference doesn't survive out of the ejb part into the web part.

                Sorry Tom

                • 5. Re: Problem returning a string from a stateless session bean
                  robisz

                  Have you seen what is in 72nd line of your LoginAction's code??????????

                  • 6. Re: Problem returning a string from a stateless session bean
                    robisz

                    Sorry check line 57 instead... Something is null there

                    • 7. Re: Problem returning a string from a stateless session bean
                      tomjmalone

                      its Object object = initialContext.lookup("foi/Login");

                      so sorry to keep asking but why can't it find my bean.

                      Tom

                      • 8. Re: Problem returning a string from a stateless session bean
                        robisz

                        Check your code thoughtfully...

                        NullPointerException in this line means that initialContext is null...
                        Probably you don't initialize it...

                        But if you can't find a solution for such a problem by yourself
                        I don't recommend you to work with J2EE.