Hibernate - Eclipse Null pointer exception [message #1453677] |
Mon, 27 October 2014 09:55 |
Marco Boldi Messages: 5 Registered: May 2013 |
Junior Member |
|
|
I'm working with a Java project that uses HSQLDB (2.3.2) and Hibernate (4.3.6) via Eclipse (Luna) using Java JDK 64 bits 1.8.0.25 in windows 7, I prepared a very small and simple test class that queries the Database.
As far as I run the application as a standard java application (that is from command line) everything works fine but when I try to debug or run the application from Eclipse I get an error during the Registry builder initialization (StandardServiceRegistryBuilder....):
Configuration c1= new Configuration();
c1.configure("/hibernate.cfg.xml");
serviceRegistry = new StandardServiceRegistryBuilder().applySettings( c1.getProperties()).build();
return c1.buildSessionFactory(serviceRegistry );
The error is:
Quote:ott 24, 2014 3:29:39 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.5.Final}
ott 24, 2014 3:29:39 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.6.Final}
ott 24, 2014 3:29:39 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000205: Loaded properties from resource hibernate.properties: {hibernate.bytecode.use_reflection_optimizer=false}
ott 24, 2014 3:29:39 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
ott 24, 2014 3:29:40 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
ott 24, 2014 3:29:40 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
ott 24, 2014 3:29:41 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: test/db/generated/MainConfParams.hbm.xml
ott 24, 2014 3:29:42 PM org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
Initial SessionFactory creation failed.java.lang.NullPointerException
Exception in thread "main" java.lang.ExceptionInInitializerError
at test.db.HibernateUtil.buildSessionFactory(HibernateUtil.java:28)
at test.db.HibernateUtil.getSessionFactory(HibernateUtil.java:34)
at test.db.Test.createAndStoreEvent(Test.java:26)
at test.db.Test.main(Test.java:17)
Caused by: java.lang.NullPointerException
at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl$AggregatedClassLoader.getResources(ClassLoaderServiceImpl.java:186)
at java.util.ServiceLoader$LazyIterator.hasNextService(ServiceLoader.java:348
at java.util.ServiceLoader$LazyIterator.hasNext(ServiceLoader.java:393)
at java.util.ServiceLoader$1.hasNext(ServiceLoader.java:474)
at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.loadJavaServices(ClassLoaderServiceImpl.java:339)
at org.hibernate.integrator.internal.IntegratorServiceImpl.<init>(IntegratorServiceImpl.java:57)
at org.hibernate.boot.registry.BootstrapServiceRegistryBuilder.build(BootstrapServiceRegistryBuilder.java:247)
at org.hibernate.boot.registry.StandardServiceRegistryBuilder.<init>(StandardServiceRegistryBuilder.java:73)
at test.db.HibernateUtil.buildSessionFactory(HibernateUtil.java:22)
... 3 more
Could you please help me ? I think it is not related to the properties files as they are parsed and used correctly.
Thanks
[Updated on: Mon, 27 October 2014 09:57] Report message to a moderator
|
|
|
|
Re: Hibernate - Eclipse Null pointer exception [message #1468070 is a reply to message #1455187] |
Mon, 10 November 2014 14:52 |
Stephan Struckmann Messages: 4 Registered: November 2014 |
Junior Member |
|
|
Thank you for guiding Marco (and me) to the Hibernate developers. I can reproduce the problem and debugged it a little bit. Finally, it is not a bug in Eclipse. However, I come now with a suggestion about the Eclipse class loader used when starting Java applications from the IDE.
In org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl (hibernate 4.3.1) in line 95 (source code repository also contains this line: see github.com/hibernate/hibernate-orm/blob/master/hibernate-core/src/main/java/org/hibernate/boot/registry/classloading/internal/ClassLoaderServiceImpl.java#L93) the class loader of ClassLoaderServiceImpl is added to the list of available class loaders, which is later used for resource loading (ie finding the hibernate.cfg.xml file).
orderedClassLoaderSet.add( ClassLoaderServiceImpl.class.getClassLoader() );
Later, in line 188 (source code repository line 186 at github.com/hibernate/hibernate-orm/blob/master/hibernate-core/src/main/java/org/hibernate/boot/registry/classloading/internal/ClassLoaderServiceImpl.java#L186) we have the following:
for ( ClassLoader classLoader : individualClassLoaders ) {
final Enumeration<URL> urls = classLoader.getResources( name );
while ( urls.hasMoreElements() ) {
resourceUrls.add( urls.nextElement() );
}
}
This throws a null pointer exception for the Eclipse class loader, which makes java.lang.Class.getClassLoader() returning null according to the javadoc of the class Class:
Quote:
getClassLoader()
(...)
Returns the class loader for the class. Some implementations may use null to represent the bootstrap class loader. This method will return null in such implementations if this class was loaded by the bootstrap class loader.
(...)
Maybe, it is possible for Eclipse classloader not to opt for the null-return????
Of course, the problem is clearly in Hibernate, and now I'll post a bug report there, but returning null somehow is a bit unexpected for receiving the loader that loaded a class which is available, even if this was specified and it may cause similar problems in the future.
Thank you again for reading my comments and for your great forum,
Stephan
|
|
|
|
|
|
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.05780 seconds