Forums Register Login

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

+Pie Number of slices to send: Send
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!
+Pie Number of slices to send: Send
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());
}
+Pie Number of slices to send: Send
thanks you are right!
Does this tiny ad smell okay to you?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 804 times.
Similar Threads
Primary Key generator bean
What might be the problem??
Help Please
problem in entity bean with local interfaces
a session bean contacts an entity bean
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 06:59:01.