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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Bear Bibeault
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • salvin francis
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
Bartenders:
  • Jj Roberts
  • Carey Brown
  • Scott Selikoff

java.lang.NullPointerException: EJB 2.0

 
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hi All:

I am just trying out examples of entity beans. I am using JBOSS 4.0.0, EJB 2.0, MyEclipse 8.6. Reason for using JBOSS 4.0.0 and EJB 2.0 is just for learning sake. I am reading head first book. I am getting this error:

java.lang.NullPointerException
at org.jboss.ejb.plugins.local.LocalHomeProxy.invoke(LocalHomeProxy.java:118)
at $Proxy0.findByPrimaryKey(Unknown Source)
at AccesesPackage.DirectorMovie.go(DirectorMovie.java:24)
at AccesesPackage.DirectorMovie.main(DirectorMovie.java:13)

My ejb.jar file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN" "http://java.sun.com/dtd/ejb-jar_2_0.dtd">
<ejb-jar>
<display-name>EJB1</display-name>
<enterprise-beans>

<session>
<display-name>AdviceBean</display-name>
<ejb-name>AdviceBean</ejb-name>
<home>headfirst.AdviceHome</home>
<remote>headfirst.Advice</remote>
<ejb-class>headfirst.AdviceBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Bean</transaction-type>
</session>

<session>
<display-name>AdviceStatefullBean</display-name>
<ejb-name>AdviceStatefullBean</ejb-name>
<home>headfirstStatefull.AdviceHomeStatefull</home>
<remote>headfirstStatefull.AdviceStatefull</remote>
<ejb-class>headfirstStatefull.AdviceStatefullBean</ejb-class>
<session-type>Stateful</session-type>
<transaction-type>Bean</transaction-type>
</session>

<entity>
<display-name>MovieBean</display-name>
<ejb-name>MovieBean</ejb-name>
<local-home>movie.MovieHome</local-home>
<local>movie.Movie</local>
<ejb-class>movie.MovieBean</ejb-class>
<persistence-type>Container</persistence-type>
<prim-key-class>java.lang.Integer</prim-key-class>
<reentrant>False</reentrant>
<cmp-version>2.x</cmp-version>
<abstract-schema-name>movie</abstract-schema-name>
<cmp-field><field-name>movieId</field-name></cmp-field>
<cmp-field><field-name>title</field-name></cmp-field>
<cmp-field><field-name>year</field-name></cmp-field>
<cmp-field><field-name>genre</field-name></cmp-field>
<cmp-field><field-name>directorId</field-name></cmp-field>
<primkey-field>movieId</primkey-field>
<security-identity>
<use-caller-identity></use-caller-identity>
</security-identity>

<query>
<query-method>
<method-name>findByPrimaryKey</method-name>
<method-params>
<method-param>java.lang.Integer</method-param>
</method-params>
</query-method>
<ejb-ql>SELECT OBJECT (m) FROM movie m WHERE m.movieId = ?1</ejb-ql>
</query>

<query>
<query-method>
<method-name>findByGenre</method-name>
<method-params>
<method-param>java.lang.String</method-param>
</method-params>
</query-method>
<ejb-ql>SELECT OBJECT (m) FROM movie m WHERE m.genre = ?1</ejb-ql>
</query>

<query>
<query-method>
<method-name>ejbSelectGetAllMovies</method-name>
<method-params>
</method-params>
</query-method>
<ejb-ql>SELECT OBJECT (m) FROM movie m</ejb-ql>
</query>

</entity>

<entity>
<display-name>DirectorBean</display-name>
<ejb-name>DirectorBean</ejb-name>
<local-home>director.DirectorHome</local-home>
<local>director.Director</local>
<ejb-class>director.DirectorBean</ejb-class>
<persistence-type>Container</persistence-type>
<prim-key-class>java.lang.Integer</prim-key-class>
<reentrant>False</reentrant>
<cmp-version>2.x</cmp-version>
<abstract-schema-name>director</abstract-schema-name>
<cmp-field><field-name>directorId</field-name></cmp-field>
<cmp-field><field-name>oscarWinner</field-name></cmp-field>
<cmp-field><field-name>degrees</field-name></cmp-field>
<cmp-field><field-name>name</field-name></cmp-field>
<cmp-field><field-name>moviesByDirector</field-name></cmp-field>
<primkey-field>directorId</primkey-field>
<security-identity>
<use-caller-identity></use-caller-identity>
</security-identity>

<query>
<query-method>
<method-name>findByPrimaryKey</method-name>
<method-params>
<method-param>java.lang.Integer</method-param>
</method-params>
</query-method>
<ejb-ql>SELECT OBJECT (d) FROM director d WHERE d.directorId = ?1</ejb-ql>
</query>

<query>
<query-method>
<method-name>ejbSelectGetAllMovies</method-name>
<method-params>
<method-param>java.lang.Integer</method-param>
</method-params>
</query-method>
<ejb-ql>SELECT OBJECT (m) FROM movie m WHERE m.directorId = ?1</ejb-ql>
</query>

</entity>



</enterprise-beans>

<relationships>
<ejb-relation>

<ejb-relationship-role>
<ejb-relationship-role-name>DirectorBean</ejb-relationship-role-name>
<multiplicity>One</multiplicity>
<relationship-role-source><ejb-name>DirectorBean</ejb-name></relationship-role-source>
<cmr-field>
<cmr-field-name>moviesByDirector</cmr-field-name>
<cmr-field-type>java.util.Collection</cmr-field-type>
</cmr-field>
</ejb-relationship-role>

<ejb-relationship-role>
<ejb-relationship-role-name>MovieBean</ejb-relationship-role-name>
<multiplicity>Many</multiplicity>
<cascade-delete />
<relationship-role-source><ejb-name>MovieBean</ejb-name></relationship-role-source>
<cmr-field>
<cmr-field-name>Director</cmr-field-name>
<cmr-field-type>java.util.Collection</cmr-field-type>
</cmr-field>
</ejb-relationship-role>

</ejb-relation>
</relationships>

</ejb-jar>

In my director package I have these files:

director home:

package director;

import javax.ejb.*;

import java.util.*;

public interface DirectorHome extends EJBLocalHome {

public Director create(Integer directorId, boolean isOscar, Integer degrees, String name) throws CreateException ;

public Collection listOfActionMoviesByDirector(Integer directorId, String genre) throws FinderException;

public Collection listOfMoviesByDirector(Integer directorId) throws FinderException;

public Director findByPrimaryKey(Integer directorId) throws FinderException;

public String established();
}


director component interface:

package director;

import javax.ejb.*;

import java.util.*;

public interface Director extends EJBLocalObject {

public void changeOscarStatus(boolean b);


}

director bean:

package director;

import java.rmi.RemoteException;
import javax.ejb.*;

import java.util.*;
import javax.sql.ConnectionPoolDataSource;
import javax.sql.PooledConnection;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import AccesesPackage.ConnectionPool;

public abstract class DirectorBean implements EntityBean {

private EntityContext context;

public void ejbActivate() {
System.out.println("Inside ejb activate method");
}

public void ejbLoad() {
System.out.println("Inside ejb activate method");
}

public void ejbPassivate() {
System.out.println("Inside ejb passivate method");
}

public void ejbRemove() {
System.out.println("Inside ejb remove method");
}

public void ejbStore() {
System.out.println("Inside ejb store method");
}

public void setEntityContext(EntityContext arg0) {
this.context = arg0;
System.out.println("Inside ejb set entity context method");
}

public void unsetEntityContext() {
System.out.println("Inside ejb unset entity context method");
}

public abstract void setDirectorId(Integer id);
public abstract Integer getDirectorId();
public abstract void setOscarWinner(boolean b);
public abstract boolean getOscarWinner();
public abstract void setDegrees(Integer m);
public abstract Integer getDegrees();
public abstract void setName(String name);
public abstract String getName();
public abstract void setMoviesByDirector(Collection movies);
public abstract Collection getMoviesByDirector();


public abstract Collection ejbSelectGetAllMovies(Integer directorId) throws FinderException;


public Collection ejbHomeListOfMoviesByDirector(Integer directorId){
Collection col = null;
try{
col = this.ejbSelectGetAllMovies(directorId);
}catch(Exception ex){
ex.printStackTrace();
}
return col;
}

public Collection ejbHomeListOfActionMoviesByDirector(Integer directorId, String genre){
return getMoviesByDirector();
}


public java.lang.Integer ejbCreate(Integer directorId, boolean isOscar, Integer degrees, String name) throws CreateException {
this.setDirectorId(directorId);
this.setOscarWinner(isOscar);
this.setDegrees(degrees);
this.setName(name);
return directorId;
}

public void ejbPostCreate(Integer directorId, boolean isOscar, Integer degrees, String name) throws CreateException {
this.setMoviesByDirector(this.ejbHomeListOfMoviesByDirector(directorId));
}



public void changeOscarStatus(boolean b){
Connection con= null;
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/ejbpr","root","chaitanya");
String sql1 = "UPDATE director SET OscarWinner = ? where DirectorId = ?";
PreparedStatement pstmt = conn.prepareStatement(sql1);
pstmt.setBoolean(1, b);
pstmt.setString(2, this.getDirectorId().toString());
boolean update = pstmt.execute();
System.out.println(update);
}catch(Exception ex){
ex.printStackTrace();
}
}

public String ejbHomeEstablished(){
return "Connection established with stub";
}


}


My movie package has following files:

movie home:

package movie;

import javax.ejb.*;
import java.util.*;
import java.rmi.*;

public interface MovieHome extends EJBLocalHome {

public Movie create(Integer movieId, String title, Integer year, String genre, Integer directorId) throws CreateException;

public Movie findByPrimaryKey(Integer Key) throws FinderException;

public Collection findByGenre(String genre) throws FinderException;

public String listAllMovies() throws FinderException;
}

movie component interface:

package movie;

import javax.ejb.*;

public interface Movie extends EJBLocalObject {

public String getMovieTitle();

}


movie bean:

package movie;

import java.rmi.RemoteException;
import javax.ejb.*;
import director.*;
import java.util.*;
import javax.naming.*;

public abstract class MovieBean implements EntityBean {

private EntityContext context = null;

public void ejbActivate() {
System.out.println("Inside ejb activate method");
}

public void ejbLoad() {
System.out.println("Inside ejb activate method");
}

public void ejbPassivate() {
System.out.println("Inside ejb passivate method");
}

public void ejbRemove() {
System.out.println("Inside ejb remove method");
}

public void ejbStore() {
System.out.println("Inside ejb store method");
}

public void setEntityContext(EntityContext arg0) {
this.context = arg0;
System.out.println("Inside ejb set entity context method");
}

public void unsetEntityContext() {
System.out.println("Inside ejb unset entity context method");
}

public String getMovieTitle(){
return this.getTitle();
}

public abstract Integer getMovieId();
public abstract void setMovieId(Integer movieId);
public abstract String getTitle();
public abstract void setTitle(String title);
public abstract Integer getYear();
public abstract void setYear(Integer year);
public abstract String getGenre();
public abstract void setGenre(String genre);
public abstract void setDirectorId(Integer directorId);
public abstract Integer getDirectorId();
public abstract Director getDirector();
public abstract void setDirector(Director director);

public abstract Collection ejbSelectGetAllMovies() throws FinderException;

public String ejbHomeListAllMovies(){
String list = null;
try{
Collection movies = this.ejbSelectGetAllMovies();
Iterator ita = movies.iterator();
while(ita.hasNext()){
Movie movie = (Movie) ita.next();
list += " " + movie.getMovieTitle();
}
}catch(Exception exe){
exe.printStackTrace();
}
return list;
}


public java.lang.Integer ejbCreate(Integer movieId, String title, Integer year, String genre, Integer directorId) throws CreateException {
this.setMovieId(movieId);
this.setTitle(title);
this.setYear(year);
this.setGenre(genre);
this.setDirectorId(directorId);
return movieId;
}

public void ejbPostCreate(Integer movieId, String title, Integer year, String genre, Integer directorId) throws CreateException{
try{
InitialContext ctx = new InitialContext();
DirectorHome dirhome = (DirectorHome) ctx.lookup("DirectorHome");
Director dir = dirhome.findByPrimaryKey(directorId);
this.setDirector(dir);
}catch(Exception ex){
ex.printStackTrace();
}
}

}


Finally I have a client class like this:

package AccesesPackage;
import javax.naming.*;
import java.rmi.*;
import javax.rmi.*;
import movie.*;
import director.*;
import javax.ejb.*;
import java.util.Properties;

public class DirectorMovie {

public static void main(String[] args){
new DirectorMovie().go();
}

public void go(){
try{
Properties properties = new Properties();
properties.put("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");
properties.put("java.naming.factory.url.pkgs","org.jboss.namingrg.jnp.interfaces");
properties.put("java.naming.provider.url","localhost:1099");
Context ic = new InitialContext(properties);
DirectorHome dir = (DirectorHome)ic.lookup("directors");
Director direct = dir.findByPrimaryKey(new Integer("1"));
}catch(Exception ex){
ex.printStackTrace();
}
}
}


Any help is greatly appreciated. I get some confidence going by solving this example.

Thank you in advance.
 
Sheriff
Posts: 6762
1283
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Duplicate topic. Please continue the discussion in your other topic. I'm locking this one.
In future, please UseOneThreadPerQuestion
 
Thank you my well lotioned goddess! Here, have my favorite tiny ad!
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
    Bookmark Topic Watch Topic
  • New Topic