1

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)

enter image description here

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

enter image description here

Source view of the persistence.xml file

enter image description here

Troller
  • 1,108
  • 8
  • 29
  • 47

1 Answers1

0

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.

Community
  • 1
  • 1
bdulac
  • 1,686
  • 17
  • 26
  • thank you for your reply. I have tried that as well but it didn't work. – Troller Dec 05 '14 at 12:54
  • I am not sure to understand your environment. It seems you are in an application server (because of EJBs). But according to the stacktrace your exception is cause directly from the Web layer (catalina/servlet is a tomcat pile). If you try to inject directly JPA components in the Web layer, this would not succeed. – bdulac Dec 05 '14 at 13:26
  • If the injection does not work, you could try a basic EMF creation: [Persistence.createEntityManagerFactory("BookStore-ejbPU")](https://docs.oracle.com/javaee/6/api/javax/persistence/Persistence.html#createEntityManagerFactory(java.lang.String)), for example in the BookFacade create method... – bdulac Dec 08 '14 at 10:35