简体   繁体   English

实体对象持续存在时,出现空指针异常

[英]Null Pointer exception when entity object is getting Persisted

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. 如上图所示,调用create方法时,将引发null ponter异常。 I guess the exception is thrown from the entitymanager. 我猜异常是从entitymanager抛出的。 Am calling the create method from a servlet which i have not shown the code. 我从一个没有显示代码的servlet调用create方法。

I am new to working with JPA ,ejb and session beans so not very clear of the errors. 我是刚接触JPA,ejb和会话Bean的人,因此对这些错误不太清楚。 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 下面是persistence.xml文件的屏幕截图

在此处输入图片说明

Source view of the persistence.xml file persistence.xml文件的源视图

在此处输入图片说明

It seems your BookFaçade class is shortened, the #create(...) method is not on line 36. 看来您的BookFaçade类已缩短,#create(...)方法不在第36行。

I guess your NullPointerException is on the access to the EntityManager. 我猜你的NullPointerException在访问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. 尝试注入EntityManagerFactory而不是EntityManager。 Then create your EntityManager locally (from the emf) in your #create(...) method. 然后在#create(...)方法中本地(从emf)创建EntityManager。 The link above has an example. 上面的链接有一个示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM