I am trying to store an entity object to the db. But my problem is when entity object is getting persisted, it throws a null pointer exception.
Below is my entity class
@Entity
@Table(name = "BOOK")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Book.findAll", query = "SELECT b FROM Book b"),
@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.findByBookdesc", query = "SELECT b FROM Book b WHERE b.bookdesc = :bookdesc"),
@NamedQuery(name = "Book.findByBookprice", query = "SELECT b FROM Book b WHERE b.bookprice = :bookprice"), })
public class Book implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 5)
@Column(name = "BOOKID")
private String bookid;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 200)
@Column(name = "BOOKNAME")
private String bookname;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 200)
@Column(name = "BOOKDESC")
private String bookdesc;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 200)
@Column(name = "BOOKPRICE")
private String bookprice;
...rest of the methods
In the below code it commits to do the transaction but then it throws the null pointer exception error.
@Stateless
public class BookFacade extends AbstractFacade<Book> implements BookFacadeLocal {
@PersistenceContext(unitName = "BookStore-ejbPU")
private EntityManager em;
@Override
protected EntityManager getEntityManager() {
return em;
}
public BookFacade() {
super(Book.class);
}
@Override
public void create(Book book)
{
em.getTransaction().begin();
em.persist(book);
em.getTransaction().commit();
}
}
Below is a screen shot of the null pointer error am getting. (Glass fish log)
As you can see in the above image, when the create method is called the null ponter exception is thrown. I guess the exception is thrown from the entitymanager. Am calling the create method from a servlet which i have not shown the code.
I am new to working with JPA ,ejb and session beans so not very clear of the errors. If you want to see more code of the program to fix the error , please comment below.
Thank you for your time.
EDIT:
Below is a screen shot of the persistence.xml file
Source view of the persistence.xml file
It seems your BookFaçade class is shortened, the #create(...) method is not on line 36.
I guess your NullPointerException is on the access to the EntityManager. Perhaps your problem is similar to this one. This would be related to your environment (which application server, which specification version).
Try injecting the EntityManagerFactory instead of an EntityManager. Then create your EntityManager locally (from the emf) in your #create(...) method. The link above has an example.