This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes EJB and other Java EE Technologies and the fly likes why ejbCreate can't lookup.......... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "why ejbCreate can Watch "why ejbCreate can New topic
Author

why ejbCreate can't lookup..........

zb cong
Ranch Hand

Joined: Jan 14, 2002
Posts: 416
hello
i have a entity bean(AuthorsBean),a session bean(FacadeBean),and a test client generated by jb6,the procedure is:
the client call the session bean,then the session bean call the entity bean.
in my session bean,i want to look up the home interface of the entity bean in the ejbCreate method,the code as follow:

public class FacadeBean implements SessionBean {
private AuthorsHome authorsHome=null;
SessionContext sessionContext;
public void ejbCreate() throws CreateException {
try{

System.out.println("this is ejbCreate");

Context ctx=new InitialContext();
Object ref=ctx.lookup("java:comp/env/ejb/Authors");
AuthorsHome authorsHome=(AuthorsHome)ref;


}catch(Exception e){
System.out.println(e.getMessage());
}
}
...........................
..........................
public java.lang.String getAddress()throws Exception{

Authors authors=authorsHome.findByPrimaryKey("172-32-1176");
return authors.getAddress();
}
}
}

my test client as:

............
.............
Context ctx = new InitialContext();
Object ref = ctx.lookup("Facade");
facadeHome = (FacadeHome) PortableRemoteObject.narrow(ref, FacadeHome.class);
Facade facade=facadeHome.create();
System.out.println(facade.getAddress());

..................
..................


it throw a null point exception.but if i modify the session bean to move the code block that is used to look up the "AuthorsHome" in ejbCreate method to the "getAddress" method,it works well,as follow:


public class FacadeBean implements SessionBean {
private AuthorsHome authorsHome=null;
SessionContext sessionContext;
public void ejbCreate() throws CreateException {
try{

System.out.println("this is ejbCreate");

}catch(Exception e){
System.out.println(e.getMessage());
}
}
...........................
..........................
public java.lang.String getAddress()throws Exception{

Context ctx=new InitialContext();
Object ref=ctx.lookup("java:comp/env/ejb/Authors");
AuthorsHome authorsHome=(AuthorsHome)ref;

Authors authors=authorsHome.findByPrimaryKey("172-32-1176");

return authors.getAddress();
}
}
}

why the ejbCreate can't lookup the home interface?

thanks for any helps!
zingaroo
Greenhorn

Joined: Apr 05, 2002
Posts: 6
u r creating a local reference to the AuthorsHme in your ejbCreate as well as getAddress() methods.
public void ejbCreate() throws CreateException {
try{
System.out.println("this is ejbCreate");
Context ctx=new InitialContext();
Object ref=ctx.lookup(".....");
AuthorsHome authorsHome=(AuthorsHome)ref;
// this is where the problem is.
// assigned the homeReference to the class
// variable authorsHome
// it should be
// authorsHome=(AuthorsHome)ref;
// hence the problem in getAddress();
// authorsHome is still null;

}catch(Exception e){
System.out.println(e.getMessage());
}
zb cong
Ranch Hand

Joined: Jan 14, 2002
Posts: 416
thanks you are right!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why ejbCreate can't lookup..........
 
Similar Threads
a session bean contacts an entity bean
Primary Key generator bean
why ejbCreate can't be invoked?
What might be the problem??
Help Please