File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes EJB and other Java EE Technologies and the fly likes InitialContext.lookup and narrow malfunctioning Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "InitialContext.lookup and narrow malfunctioning" Watch "InitialContext.lookup and narrow malfunctioning" New topic

InitialContext.lookup and narrow malfunctioning

Murad Iqbal
Ranch Hand

Joined: Dec 09, 2003
Posts: 90
Hi all,

I have successfully deployed a simple CMP entity bean on JBoss 3.2.3. I am attempting to access the ejb using the following client code. both the client and the jboss server that has the ejb container are on the same machine. However, I experienced a weird behavior from the lookup method. When i did Object obj = ctx.lookup("local/PersonnelBean"), it should return the LocalHome interface of the PersonnelBean which is PersonnelLocalHome (same name in ejb-jar.xml), but it returned me local/PersonnelBeanHome which doesnt exist at all. I got this by printint out the obj object. Thereafter, I changed my PersonnelLocalHome interface to PersonnelBeanHome just to let it go to the next step, but it is giving me the same NullPointer Exception. Kindly help. 'local/PersonnelBean' is the jndi name of the bean as told by JBoss. Following is the client code.

public static void main( String [] args )

Properties prop = new Properties();
prop.setProperty(Context.PROVIDER_URL, "localhost:1099");
"org.jboss.naming rg.jnp.interfaces");

Context ctx = new InitialContext(prop);
System.out.println("TestEJB: 1 ");

Object obj = ctx.lookup( "local/PersonnelBean" );
System.out.println("Obj is: " + obj);
System.out.println("TestEJB: 2");

com.btm.ejb.personnel.PersonnelBeanHome home =
obj, com.btm.ejb.personnel.PersonnelBeanHome.class );
System.out.println("TestEJB: home object created");
PersonnelLocal personnel = home.create("M001", "Murad", "Iqbal",
"4343", 1, "SE", "PK", "19121979");
System.out.println("TestEJB: object created");
//System.out.println( personnelLocal.hello());
catch ( Exception e )
System.out.println( "Exception: " + e );

TestEJB: 1
Obj is: local/PersonnelBeanHome
TestEJB: 2
TestEJB: home object created
Exception: java.lang.NullPointerException
at org.jboss.ejb.plugins.local.LocalHomeProxy.invoke(
at $Proxy0.create(Unknown Source)
at com.test.TestEJB.main(
Avi Abrami
Ranch Hand

Joined: Oct 11, 2000
Posts: 1141

It seems to me that you have misunderstood the purpose of the "local" EJB interfaces (as opposed to the "remote" interfaces). The "local" interface is _not_ for clients that are on the same machine as the EJB container. The local interface is for clients that are in the same JVM as the EJB container. Since I assume your client is not in the same JVM as the EJB container, it is a remote client, and must therefore use the EJB "remote" interface.

The local interface was introduced so that one type of EJB (for example a session bean) could lookup a different type of EJB (for example an entity bean) in the same EJB container.

I think you also need to define remote interfaces for your EJB's -- since it appears you have currently only defined local interfaces.

For your information, TheServerSide Web site has a freely available book on EJB -- which can be downloaded as a single, PDF file.

Good Luck,
It is sorta covered in the JavaRanch Style Guide.
subject: InitialContext.lookup and narrow malfunctioning
It's not a secret anymore!