6 Replies Latest reply on Jun 9, 2008 6:39 AM by jaikiran

    NamingException: Could not dereference object - EJB 3.0 Stat

    htran_888

      Hi All,

      I am receiving the following error when trying to reference 2 Stateful Session beans (TransactionPersistenceContext & ExtendedPersistenceContext):
      --------------------------------------------------------------------------------------
      Distributing D:\Test\EJB3ex511-client\dist\EJB3ex511-client.jar to [org.jboss.deployment.spi.LocalhostTarget@13aaba1]
      Deploying D:\Test\EJB3ex511-client\dist\EJB3ex511-client.jar
      Deploying D:\Test\EJB3ex511-client\dist\EJB3ex511-client.jar
      Applicaton Deployed
      Operation start started
      Operation start completed
      run-deploy:
      run-tool:
      run-jar:
      log4j:WARN No appenders could be found for logger (org.jboss.security.SecurityAssociation).
      log4j:WARN Please initialize the log4j system properly.
      no cabin should be null: null
      Master Suite
      1
      1
      3
      Updating detached cabin instance with new bed count of 4
      Finding cabin to see it has been updated with a merge() on server
      new bed count is: 4
      javax.naming.NamingException: Could not dereference object [Root exception is java.lang.reflect.UndeclaredThrowableException] at org.jnp.interfaces.NamingContext.getObjectInstanceWrapFailure(NamingContext.java:1150)
      at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:705)
      at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:587)
      at javax.naming.InitialContext.lookup(InitialContext.java:392)
      at ejb3ex511client.Main.main(Main.java:50)
      Caused by: java.lang.reflect.UndeclaredThrowableException
      at $Proxy1.createProxy(Unknown Source)
      at org.jboss.ejb3.JndiProxyFactory.getObjectInstance(JndiProxyFactory.java:52)

      --------------------------------------------------------------------------------------

      Here are the codes for 1 Stateless (TravelAgentBean - pre-requisite) & 1 Stateful (TransactionPersistenceContextBean) session beans and a Client manipulating these beans:

      @Stateless
      public class TravelAgentBean implements TravelAgentRemote
      {
       @PersistenceUnit(unitName="EJB3ex51") private EntityManagerFactory factory;
       @PersistenceContext(unitName="EJB3ex51") private EntityManager manager;
      
       public void createCabin(Cabin cabin)
       {
       manager.persist(cabin);
       }
      
       public Cabin findCabin(int pKey)
       {
       return manager.find(Cabin.class, pKey);
       }
      
       public void updateCabin(Cabin cabin)
       {
       manager.merge(cabin);
       }



      @Stateful
      public class TransactionPersistenceContextBean implements TransactionPersistenceContextRemote
      {
       @PersistenceContext(unitName="titan", type=PersistenceContextType.TRANSACTION)
       private EntityManager manager;
      
       private Cabin cabin;
      
       public void setCabin(int pk)
       {
       cabin = manager.find(Cabin.class, pk);
       }
      
       public void updateBedCount(int newBedCount)
       {
       cabin.setBedCount(newBedCount);
       }
      
       @Remove
       public void remove()
       {
       }
      }


      public static void main(String[] args) {
      
       try
       {
       Context jndiContext = getInitialContext();
       Object ref = jndiContext.lookup("travelagent.TravelAgentRemote");
       TravelAgentRemote dao = (TravelAgentRemote)ref;
      
       Cabin noCabin = dao.findCabin(1);
       System.out.println("no cabin should be null: " + noCabin);
      
       Cabin cabin_1 = new Cabin();
       cabin_1.setId(1);
       cabin_1.setName("Master Suite");
       cabin_1.setDeckLevel(1);
       cabin_1.setShipId(1);
       cabin_1.setBedCount(3);
      
       dao.createCabin(cabin_1);
      
       Cabin cabin_2 = dao.findCabin(1);
       System.out.println(cabin_2.getName());
       System.out.println(cabin_2.getDeckLevel());
       System.out.println(cabin_2.getShipId());
       System.out.println(cabin_2.getBedCount());
      
       System.out.println("Updating detached cabin instance with new bed count of 4");
       cabin_2.setBedCount(4);
       dao.updateCabin(cabin_2);
      
       System.out.println("Finding cabin to see it has been updated with a merge() on server");
       Cabin cabin_3 = dao.findCabin(1);
       System.out.println("new bed count is: " + cabin_3.getBedCount());
      
       ref = jndiContext.lookup("travelagent.TransactionPersistenceContextRemote");
       TransactionPersistenceContextRemote txBean = (TransactionPersistenceContextRemote)ref;
      
       Cabin fetchedCabin = dao.findCabin(1);
       int oldBedCount = fetchedCabin.getBedCount();
      
       System.out.println("Set up transaction persistence context stateful bean");
       txBean.setCabin(1);
       txBean.updateBedCount(5);
      
       fetchedCabin = dao.findCabin(1);
       System.out.println("Cabin bed count will still be " + oldBedCount + ": " + fetchedCabin.getBedCount());
      
       System.out.println("Set up extended persistence context stateful bean");
      
       ref = jndiContext.lookup("travelagent.ExtendedPersistenceContextRemote");
       ExtendedPersistenceContextRemote extendedBean = (ExtendedPersistenceContextRemote)ref;
      
       extendedBean.setCabin(1);
       extendedBean.updateBedCount(5);
      
       fetchedCabin = dao.findCabin(1);
       System.out.println("Cabin bed count will be 5: " + fetchedCabin.getBedCount());
      
       // cleanup
       txBean.remove();
       extendedBean.remove();
      
       }
       catch (javax.naming.NamingException ne)
       {
       ne.printStackTrace();
       }
       }
      
       public static Context getInitialContext()
       throws javax.naming.NamingException
       {
       return new javax.naming.InitialContext();
       }


      I have tried to split the client into separate classes but the outcome is still the same.

      I am running Netbeans 5.5, JBoss 4.0.5 AS on Windows XP platform.

      I have struggled with this issue for many days after trying different method of deployment including using both sun-appserver-pe9.0 & JBoss 4.0.5 AS without success.

      This exercise is from workbook (ex05_1), title EJB 3.0 by Bill Burke.

      Any assistance would be very much appreciated.

      Thanks,

      Henry

        • 1. Re: NamingException: Could not dereference object - EJB 3.0
          htran_888

          Hi,

          I managed to overcome this issue by carrying out the the following changes which worked on Sun System Java Application Server PE 9.0 only:

          ( i ) Re-create a single titan ejb project to include all 3 session beans (TravelAgent, TransactionPersistenceContext, ExtendedPersistenceContext) & 1 entity class (Cabin) in one titan.jar file. More importantly, have all classes pointing to 1 persistence unit (titan).

          The persistence.xml file was the root to my problem.

          ( ii ) Re-create Client with these syntax:

          ref = jndiContext.lookup("travelagent.TransactionPersistenceContextRemote");
          ref = jndiContext.lookup("travelagent.ExtendedPersistenceContextRemote");

          As a result, the Client output from Netbeans is:

          no cabin should be null: null
          Master Suite
          1
          1
          3
          Updating detached cabin instance with new bed count of 4
          Finding cabin to see it has been updated with a merge() on server
          new bed count is: 4
          Set up transaction persistence context stateful bean
          Cabin bed count will still be 4: 4
          Set up extended persistence context stateful bean
          Cabin bed count will be 5: 5

          However, I still could not find a solution when running in JBoss AS 4.0.5.

          Thanks,

          Henry

          • 2. Re: NamingException: Could not dereference object - EJB 3.0
            htran_888

            Hi All,

            Could someone please help me with this issue?

            I still could not identify what the problem for so many weeks.

            Thanks,

            Henry

            • 3. Re: NamingException: Could not dereference object - EJB 3.0
              jaikiran
              • 4. Re: NamingException: Could not dereference object - EJB 3.0
                htran_888

                Hi Jaikiran,

                Thanks for pointing me to a relevant post.

                I will try the suggestion and let everyone know.

                Cheers,

                Henry

                • 5. Re: NamingException: Could not dereference object - EJB 3.0
                  vinu15378

                  I am facing the same problem of JNDI lookup in EJB 3.0 Entity Beans.
                  I am using Netbeans 5.5.1,JBoss 4.2.0 and standalone client.
                  I have checked everything.I didnt get where should we search for JNDI name to look up for bean.
                  Struggling hard for this example.
                  Please help for the same
                  Following is the Client Code.

                  package enterpriseapplication6;

                  import com.titan.travelagent.TravelAgentRemote;
                  import com.titan.domain.Cabin;
                  import java.util.Properties;

                  import javax.naming.InitialContext;
                  import javax.naming.Context;

                  public class Client
                  {
                  public static void main(String [] args)
                  {
                  try
                  {
                  Context jndiContext = getInitialContext();
                  Object ref = jndiContext.lookup("travelagent.TransactionPersistenceContextRemote");
                  TravelAgentRemote dao = (TravelAgentRemote)ref;

                  Cabin cabin_1 = new Cabin();
                  cabin_1.setId(1);
                  cabin_1.setName("Master Suite");
                  cabin_1.setDeckLevel(1);
                  cabin_1.setShipId(1);
                  cabin_1.setBedCount(3);

                  dao.createCabin(cabin_1);

                  Cabin cabin_2 = dao.findCabin(1);
                  System.out.println(cabin_2.getName());
                  System.out.println(cabin_2.getDeckLevel());
                  System.out.println(cabin_2.getShipId());
                  System.out.println(cabin_2.getBedCount());

                  }
                  catch (javax.naming.NamingException ne)
                  {
                  ne.printStackTrace();
                  }
                  }

                  public static Context getInitialContext()
                  throws javax.naming.NamingException
                  {
                  Properties p = new Properties();
                  p.put(InitialContext.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
                  p.put(InitialContext.PROVIDER_URL,"jnp://localhost:1099");
                  InitialContext ctx = new InitialContext(p);
                  return new javax.naming.InitialContext();
                  }
                  }


                  Following error I am getting while executing Client Code.
                  I havent made any changes in Remote and Bean Class.

                  Please help me executing examples in EJB 3.0 Entity Bean Examples using
                  JBoss AS

                  javax.naming.NameNotFoundException: travelagent.TransactionPersistenceContextRemote not bound
                  at org.jnp.server.NamingServer.getBinding(NamingServer.java:529)
                  at org.jnp.server.NamingServer.getBinding(NamingServer.java:537)
                  at org.jnp.server.NamingServer.getObject(NamingServer.java:543)
                  at org.jnp.server.NamingServer.lookup(NamingServer.java:296)
                  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
                  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
                  at java.lang.reflect.Method.invoke(Method.java:597)
                  at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:305)
                  at sun.rmi.transport.Transport$1.run(Transport.java:159)
                  at java.security.AccessController.doPrivileged(Native Method)
                  at sun.rmi.transport.Transport.serviceCall(Transport.java:155)
                  at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:535)
                  at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:790)
                  at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:649)
                  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)
                  at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:255)
                  at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:233)
                  at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:142)
                  at org.jnp.server.NamingServer_Stub.lookup(Unknown Source)
                  at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:625)
                  at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:587)
                  at javax.naming.InitialContext.lookup(InitialContext.java:392)
                  at enterpriseapplication6.Client.main(Client.java:17)

                  • 6. Re: NamingException: Could not dereference object - EJB 3.0
                    jaikiran

                     

                    "vinu15378" wrote:
                    I didnt get where should we search for JNDI name to look up for bean.



                    Follow the steps mentioned at http://wiki.jboss.org/wiki/DisplayTheJNDITreeWithTheJMXConsole. On the jndi-tree contents page, search for your bean and see what's the jndi-name to which it is bound.