JPA和derby空指针异常 [英] JPA and derby null pointer exception

查看:159
本文介绍了JPA和derby空指针异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用jpa 2.0和derby进行简单的持久化,但是我一直收到NullPointerException.

I am trying to do a simple persistence with jpa 2.0 and derby but I keep getting a NullPointerException.

异常和实体类- http://pastebin.com/QqXhRdcN

我要保留的代码

EntityManager em = emf.createEntityManager();  
  Course c = new Course("cse", 214, "fall", 2010);  
  em.getTransaction().begin();  
  em.persist(c);  
  em.getTransaction().commit();  
  em.close();  

空指针异常发生在上面的代码的第一行171行.

The null pointer exception occurs at line 171 which is the first line of the above code.

提前谢谢!

推荐答案

您的EntityManagerFactory在初始化时未正确创建(因此它是null),因此在尝试使用它时是NullPointerException获得EntityManager.

Your EntityManagerFactory doesn't get properly created at initialization time (so it is null), hence the NullPointerException when you try to use it to obtain an EntityManager.

尽管EclipseLink在尝试为异常生成国际化消息时肯定不会因NPE失败(您应将其记录为错误),但跟踪仍然包含有用的信息,如下面的粗体所示:

And while EclipseLink should certainly not fail with a NPE while trying to generate an internationalized message for the exception (you should log this as a bug), the trace still contains useful information, as shown in bold below:

[EL Severe]: 2010-10-23 05:27:14.092--ServerSession(1673653)--java.lang.NullPointerException
    at java.util.ResourceBundle.getBundle(Unknown Source)
    at org.eclipse.persistence.exceptions.i18n.ExceptionMessageGenerator.buildMessage(ExceptionMessageGenerator.java:47)
    at org.eclipse.persistence.exceptions.DatabaseException.unableToAcquireConnectionFromDriverException(DatabaseException.java:376)
    at org.eclipse.persistence.sessions.DefaultConnector.connect(DefaultConnector.java:91)
    at org.eclipse.persistence.sessions.DatasourceLogin.connectToDatasource(DatasourceLogin.java:162)
    at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.loginAndDetectDatasource(DatabaseSessionImpl.java:579)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryProvider.login(EntityManagerFactoryProvider.java:228)
    at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:380)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.getServerSession(EntityManagerFactoryImpl.java:157)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManagerImpl(EntityManagerFactoryImpl.java:214)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:202)
    ...

您的连接池配置很可能出了点问题,EclipseLink无法从中获取有效的连接.

There is very likely something wrong with your connection pool configuration, EclipseLink isn't able to get a valid connection from it.

如果没有有关您的设置的更多详细信息(例如persistence.xml),Tomcat数据源配置以及您使用Derby的方式(在服务器模式下),我将无法给出更准确的答案.但这就是外观.

I can't give a more precise answer without more details about your settings like the persistence.xml, the Tomcat datasource configuration, the way you use Derby (in server mode?). But that's where to look.

这篇关于JPA和derby空指针异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆