问题
I am trying to do a simple persistence with jpa 2.0 and derby but I keep getting a NullPointerException.
Exception and entity class - http://pastebin.com/QqXhRdcN
The code where I am trying to persist
EntityManager em = emf.createEntityManager();
Course c = new Course("cse", 214, "fall", 2010);
em.getTransaction().begin();
em.persist(c);
em.getTransaction().commit();
em.close();
The null pointer exception occurs at line 171 which is the first line of the above code.
Thanks in advance!
回答1:
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
.
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)
...
There is very likely something wrong with your connection pool configuration, EclipseLink isn't able to get a valid connection from it.
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.
来源:https://stackoverflow.com/questions/4003069/jpa-and-derby-null-pointer-exception