I am getting a null pointer exception with a simple
EJB app.
My Client Code is :
package Book;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
/**
*
* @author Rohit Bhatia
*/
public class BookClient {
@EJB
private static BookFacadeLocal bc;
/** Creates a new instance of BookClient */
public BookClient() {
}
public static void main(
String[] args) {
Book book = new Book(4, "dj" , "df", "hfk" , "dkhk", "dhke");
bc.save(book);
}
My Session Bean Code is :
package Book;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
/**
*
* @author Rohit Bhatia
*/
@Stateless
public class BookFacade implements BookFacadeLocal {
@PersistenceContext
private EntityManager em;
/** Creates a new instance of BookFacade */
public BookFacade() {
}
public void create(Book book) {
em.persist(book);
}
public void edit(Book book) {
em.merge(book);
}
public void destroy(Book book) {
em.merge(book);
em.remove(book);
}
public Book find(Object pk) {
return em.find(Book.class, pk);
}
public List findAll() {
return em.createQuery("select object(o) from Book as o").getResultList();
}
public void save(Book book) {
em.persist(book);
System.out.println("book Saved");
}
My Entity class code is :
package Book;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
/**
* Entity class Book
*
* @author Rohit Bhatia
*/
@Entity
@Table(name = "book")
@NamedQueries( {
@NamedQuery(name = "Book.findByBookId", query = "SELECT b FROM Book b WHERE b.bookId = :bookId"),
@NamedQuery(name = "Book.findByBookName", query = "SELECT b FROM Book b WHERE b.bookName = :bookName"),
@NamedQuery(name = "Book.findByBookEdition", query = "SELECT b FROM Book b WHERE b.bookEdition = :bookEdition"),
@NamedQuery(name = "Book.findByBookIsbn", query = "SELECT b FROM Book b WHERE b.bookIsbn = :bookIsbn"),
@NamedQuery(name = "Book.findByBookAuthor", query = "SELECT b FROM Book b WHERE b.bookAuthor = :bookAuthor"),
@NamedQuery(name = "Book.findByBookPublication", query = "SELECT b FROM Book b WHERE b.bookPublication = :bookPublication")
})
public class Book implements Serializable {
@Id
@Column(name = "BOOK_ID", nullable = false)
private Integer bookId;
@Column(name = "BOOK_NAME", nullable = false)
private String bookName;
@Column(name = "BOOK_EDITION", nullable = false)
private String bookEdition;
@Column(name = "BOOK_ISBN", nullable = false)
private String bookIsbn;
@Column(name = "BOOK_AUTHOR", nullable = false)
private String bookAuthor;
@Column(name = "BOOK_PUBLICATION", nullable = false)
private String bookPublication;
/** Creates a new instance of Book */
public Book() {
}
/**
* Creates a new instance of Book with the specified values.
* @param bookId the bookId of the Book
*/
public Book(Integer bookId) {
this.bookId = bookId;
}
/**
* Creates a new instance of Book with the specified values.
* @param bookId the bookId of the Book
* @param bookName the bookName of the Book
* @param bookEdition the bookEdition of the Book
* @param bookIsbn the bookIsbn of the Book
* @param bookAuthor the bookAuthor of the Book
* @param bookPublication the bookPublication of the Book
*/
public Book(Integer bookId, String bookName, String bookEdition, String bookIsbn, String bookAuthor, String bookPublication) {
this.bookId = bookId;
this.bookName = bookName;
this.bookEdition = bookEdition;
this.bookIsbn = bookIsbn;
this.bookAuthor = bookAuthor;
this.bookPublication = bookPublication;
}
/**
* Gets the bookId of this Book.
* @return the bookId
*/
public Integer getBookId() {
return this.bookId;
}
/**
* Sets the bookId of this Book to the specified value.
* @param bookId the new bookId
*/
public void setBookId(Integer bookId) {
this.bookId = bookId;
}
/**
* Gets the bookName of this Book.
* @return the bookName
*/
public String getBookName() {
return this.bookName;
}
/**
* Sets the bookName of this Book to the specified value.
* @param bookName the new bookName
*/
public void setBookName(String bookName) {
this.bookName = bookName;
}
/**
* Gets the bookEdition of this Book.
* @return the bookEdition
*/
public String getBookEdition() {
return this.bookEdition;
}
/**
* Sets the bookEdition of this Book to the specified value.
* @param bookEdition the new bookEdition
*/
public void setBookEdition(String bookEdition) {
this.bookEdition = bookEdition;
}
/**
* Gets the bookIsbn of this Book.
* @return the bookIsbn
*/
public String getBookIsbn() {
return this.bookIsbn;
}
/**
* Sets the bookIsbn of this Book to the specified value.
* @param bookIsbn the new bookIsbn
*/
public void setBookIsbn(String bookIsbn) {
this.bookIsbn = bookIsbn;
}
/**
* Gets the bookAuthor of this Book.
* @return the bookAuthor
*/
public String getBookAuthor() {
return this.bookAuthor;
}
/**
* Sets the bookAuthor of this Book to the specified value.
* @param bookAuthor the new bookAuthor
*/
public void setBookAuthor(String bookAuthor) {
this.bookAuthor = bookAuthor;
}
/**
* Gets the bookPublication of this Book.
* @return the bookPublication
*/
public String getBookPublication() {
return this.bookPublication;
}
/**
* Sets the bookPublication of this Book to the specified value.
* @param bookPublication the new bookPublication
*/
public void setBookPublication(String bookPublication) {
this.bookPublication = bookPublication;
}
/**
* Returns a hash code value for the object. This implementation computes
* a hash code value based on the id fields in this object.
* @return a hash code value for this object.
*/
@Override
public int hashCode() {
int hash = 0;
hash += (this.bookId != null ? this.bookId.hashCode() : 0);
return hash;
}
/**
* Determines whether another object is equal to this Book. The result is
* <code>true</code> if and only if the argument is not null and is a Book object that
* has the same id field values as this object.
* @param object the reference object with which to compare
* @return <code>true</code> if this object is the same as the argument;
* <code>false</code> otherwise.
*/
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Book)) {
return false;
}
Book other = (Book)object;
if (this.bookId != other.bookId && (this.bookId == null || !this.bookId.equals(other.bookId))) return false;
return true;
}
/**
* Returns a string representation of the object. This implementation constructs
* that representation based on the id fields.
* @return a string representation of the object.
*/
@Override
public String toString() {
return "Book.Book[bookId=" + bookId + "]";
}
}
THE ERROR I AM GETTING IS :
init:
deps-jar:
compile-single:
run-main:
Exception in
thread "main" java.lang.NullPointerException
at Book.BookClient.main(BookClient.java:46)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)