Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
zb cong
Ranch Hand
Posts: 416
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 416
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks you are right!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic