• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

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

 
Ranch Hand
Posts: 416
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks you are right!
 
This is my favorite show. And this is my favorite tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic